This question already has answers here:
Does python optimize modules when they are imported multiple times?
(6 answers)
Closed 8 years ago.
I'm wondering about a few things concerning importing modules.
I have a module that contains nothing but a list of variables, so that I can use these across 3 or 4 scripts that run either once or daily.
This same module I would like to use in another script of mine, but I only need to load it once, and afterwards, I don't need the module anymore, because I would copy the variables to a list in my script(for comparison purposes).
My questions:
1. if I import the module in a method, is it discarded when the function ends?
2. what is the memory-impact on importing a module?
Good to know is that the function is one-shot.
Greetings
A reference to the module is stored in sys.modules, so no it's not released.
Consider using execfile or similar if you don't want to load the module
You can actually unload a module from python, it will be garbage collected if it is not referenced anymore :
del sys.modules["mymodule"]
del mymodule
Related
This question already has answers here:
Should import statements always be at the top of a module?
(22 answers)
Closed 2 years ago.
I need to support Python code, which was written by another person and it contains imports, that are placed not at the top of the module. And I'm a bit afraid to break something, as project is quite complex.
So when it can be good to use imports not on the top of the file?
If you have a python program that has different paths, and only one of the paths use a specific module, you can import the module in that specific path. So you won't be importing many module you won't use.
This question already has answers here:
How do I unload (reload) a Python module?
(22 answers)
Closed 3 years ago.
I have a script that I am in the process of debugging. I've imported it using:
import foo
Then I update foo. After I do "import foo" again, nothing is changed. How do I update it without needing to exist the interpreter and reenter? Furthermore, if there are other packages that depend on it, how do I update them to use the newest version?
Use importlib.reload(). This used to be a builtin (in Python 2), but it doesn't clean up everything. This was considered too confusing and it was moved to importlib for advanced users. Read the documentation carefully to understand why.
>>> import foo
>>> from importlib import reload
>>> # do stuff
>>> reload(foo)
Modules in Python are cached in the sys.modules dict. They're only loaded from source on the first import. If you delete it there, you can get a similar effect.
The main difference between these two approaches is that reload() keeps the same module __dict__ object (its globals), while just deleting it from sys.modules wouldn't. Normally a reload will overwrite these globals with the new definitions if you modify the source. But if you remove a definition in the source, the old version will still be there after a reload. You can actually use this to your advantage in some cases: if you want a resource (like a network connection) to persist over reloads, you can write its initialization to skip the step if the global is already defined.
This question already has answers here:
How to share imports between modules?
(2 answers)
Closed 3 years ago.
Considering the following python code setup:
main.py
MTS/
optimization.py
__init__.py
Where __init__.py consists of one line import optimization
and optimization.py uses different modules such as numpy (called as np). Let's main.py be:
from MTS import optimization
import numpy as np
a=np.load('data.npy')
b=optimization.minimize(a)
this code returns the following error:
global name 'np' is not defined
If I import numpy inside optimization.py, it works but I want to avoid importing the same module twice. How to share an imported module across other modules?
In python, imports are mainly about namespaces. Importing a module puts it into the current namespace, and if it's not already in memory it loads it into memory.
If you import a module that another module has already imported, then that module is already in memory so you just get a reference to the existing module.
So, in other words, just do import numpy as np inside every file you need to use it in. This will not consume any extra memory, as numpy will only be properly loaded once, and every subsequent import statement will just add it to the namespace of whoever calls it.
An additional benefit of this is that it's much more clear that your code is going to use numpy - it would be confusing if you just used np.load() halfway through your program and there was no indication that you'd defined np anywhere (C has this problem, for example; pretty much every programming language since has used the idea of namespaces to avoid it, including even C++). It is poor practice in python to not do this.
This question already has answers here:
How to get local variables updated, when using the `exec` call?
(3 answers)
Closed 4 years ago.
In my main script, I import one of my own module which contains global variables. This main script execute another script with the function exec (exec(compile(open(Seq_1, "rb").read(), Seq_1, 'exec')) and this other script import the same module.
So my question is: does these scripts have access to the same global variables (that means if I modify one global variable, the other script will be impacted) or not?
Python will run your file when you first import it. On the second import python won't re-run the file.
In practice, python functions and variables directly on modules (not wrapped in classes) works like singletons.
This answer explains more about it. You can directly refer to the docs, also suggested on linked answer.
This question already has answers here:
Finding where something was imported from in python
(4 answers)
Closed 8 years ago.
When using the interactive shell in Spyder, for instance, all sorts of classes are in the global namespace, so it would be nice to be able to interactively find what module the class comes from.
The answer is buried in one of the responses to the hidden features of Python question:
You can ask any object which module it came from by looking at its
__module__ property. This is useful, for example, if you're
experimenting at the command line and have imported a lot of things.
Along the same lines, you can ask a module where it came from by
looking at its __file__ property. This is useful when debugging path
issues.