1

I'm doing simulations of optical properties and started to create a package for my own use that includes all functions and object I have written so far. For obvious reasons I use numpy and I used to import numpy in my modules.

Now, with the package, I wish to make numpy globally available for every submodule. From what I've gathered so far, this might be bad practice. However, if I import numpy in each module I get numpy as a key/function for that module, which seems to be even worse practice.

In other words: When I create a minimal package with only package/__init __.py and package/module.py and import numpy in module.py, import it and print all available functions

import package
print package.module.__dict__.keys()
['__builtins__', '__file__', '__package__', 'numpy', '__name__', '__doc__']

numpy shows up. So in principle I could access numpy via

package.module.numpy

I am not sure if this is really a problem and how this is done correctly.

DNA
42.7k12 gold badges114 silver badges153 bronze badges
asked Apr 1, 2015 at 8:05
1
  • It's not really a problem. The standard modules do it too. Commented Apr 1, 2015 at 8:12

1 Answer 1

1

I agree with @Sneftel's comment -- this isn't a problem.

Since this is not part of the documented interface of your module, although the user can see there is something named numpy in the module (which is not necessarily the numpy module, it could be anything), as a grownup, the user should avoid using it.

answered Apr 1, 2015 at 8:25
Sign up to request clarification or add additional context in comments.

3 Comments

Hah, that gives me a great idea. import numpy as _calibrationNode. import os.path as numpy. Keep them guessing.
@Sneftel ... we're all supposed to be responsible grown ups! ;)
Thanks! I'm glad to be a responsible adult. I just expected to work differently.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.