Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8826809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:13:53+00:00 2026-06-14T07:13:53+00:00

I am adding some code into an existing class for testing purposes. Normally this

  • 0

I am adding some code into an existing class for testing purposes. Normally this class, eigensystem_CUDA_implementation, relies on some functions and attributes of its parent. When this class is imported independently of the rest of the program, I’d like to substitute the usual parent class for a testParent class.

If this is a bad way to test code and I should do this completely differently, I’m open to that suggestion.

When initializing an eigensystem_CUDA_implementation object, the error is:

NameError: free variable ‘np’ referenced before assignment in enclosing scope

which traces back to the line self.mat = np.matrix(...

There is much more that relies on NumPy and pyCUDA in eigensystem_CUDA_implementation that is not shown. Can a class import a module that it relies on? Haven’t tested this yet because of the first error.

class eigensystem_CUDA_implementation:
    def __init__(self, parent = None, max_time = 60, delta = 10**(-32)):
            # For testing purposes, when class is utilized independently
            if(not parent):
                    # testing mode
                    if(not sys.modules.has_key("numpy")):
                            import numpy as np
                            import pycuda.driver as cuda
                            import pycuda.autoinit
                            from pycuda.compiler import SourceModule
                    class testParent:
                            def __init__(self, size = 10):
                                    self.size = size
                                    self.delta = 10**(-32)
                                    self.num_site_types = 8
                                    self.mat =  np.matrix(np.random.random((self.size,self.size)).astype(np.float64))
                            def get_mutation_selection_matrix(self, alpha):
                                    return self.mat
    ...

One potential problem: not sys.modules.has_key("numpy") would show NumPy as imported whether it is named “np” or something else. The rest of the code uses “np” though so I’m ignoring this.

Thanks for any suggestions

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:13:56+00:00Added an answer on June 14, 2026 at 7:13 am

    Your code will fail if numpy is already imported. You only import it inside the if block, so if it’s already imported it won’t be defined inside that block. But later in the same function you reference np as a local variable.

    Anyway, you don’t really need to worry about importing numpy. Just do import numpy as np unconditionally. If it’s already imported, it will just re-use the imported version. It won’t waste memory or anything importing it twice.

    That said, this code looks rather unwieldy and fragile. You should see if there’s a better way to do this, by for instance defining a separate function that patches up the class with the necessary attributes. Having a class and an import inside a method inside another class is getting pretty hairy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been adding unit tests to some legacy C++ code, and I've run into
I am adding some code to an existing FreeBSD device driver and I am
I'm working on adding some analytics code into our e-commerce website. What I'm doing
I'm adding some code into a using block in a C# program. I'm sort
i am creating XmlDocument by code and adding some tag. The structure is like
So I'm going in and adding TAF (cluster failover) processing to some database code,
I'm looking into adding some unit tests for some classes in my data access
I'm adding some ASP.NET MVC pages to an existing ASP.NET Web Forms project. I've
I'm trying to turn some code written in Matlab into a standalone, compiled Matlab
I'm adding new features to an existing code base. Anyway, the current feature I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.