Is import Dynamic or Static [duplicate] - python

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.

Related

How do you update an imported script in the Python interpreter? [duplicate]

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.

Functions in Python vs Matlab [duplicate]

This question already has answers here:
How do I call a function from another .py file? [duplicate]
(19 answers)
Closed 6 years ago.
I'm new to Python, coming from Matlab like many. I'm used to defining my functions as standalone .m files and calling them easily from a second script as long as the function is saved somewhere within the defined Matlab path.
I've learned how to define a (user-defined) function in Python (def my_function() etc.), but I'm coming up short in my Google searches on a way to define the function in a a separate .py file A, and how to call it in another script B. All help files I can find give support on how to define the function within the same script. When I try calling the function (which I've defined as a .py file in the same folder I'm using for script A) my script doesn't recognize it. Do I need to be declaring the function at the beginning of my script? I'm getting the sense that Python is very different in the way it handles these things than Matlab--perhaps I can't do this?
Cheers
You have to use imports
for example, you have the function doSomething() in functions.py and want to work in main.py
in main.py
from functions import doSomething
var = doSomething()

Transition from MATLAB to Python [duplicate]

This question already has answers here:
Import folder of modules in Python
(3 answers)
Closed 7 years ago.
I know this question has been asked before but I can't make heads or tails of what the answer means.
I am making the transition from MATLAB to Python. In MATLAB I can write my own functions and use them in my code. I know I can do the same thing in Python. But I am having a hard time figuring out how to do it.
What I would like to do it create a file with multiple function definitions and then import it into Python like any other module.
First, is this the proper way of thinking it about it? Or do I just need to create multiple definition files for each function?
Second, if it is the proper way of thinking about it how do I access the file? I know you have to set the PYTHONPATH. I have looked at it and where it is looking makes no sense to me.
As an Example: I created a folder called User. In it I have a python function called ted.py. I put said file where the rest of the library files are located (as in numpy or scipy). I want to import the file called User. How can I do this?
After working with Python for awhile I get it. As long as the file is in the same directory and you use the import properly you can use one , some or all of the function definitions in the file.
You have an un-matlab-like (matlab-unlike? dis-matlab-like?) option of putting multiple function definitions into the same .py file. Once the file -- say, fundefs.py -- is on your path, possibly through having issued import sys; sys.path.append('path/to/fundefs');, you can import it
through import fundefs, after which you can access the functions therein by fundefs.fun1, fundefs.fun2 etc.
through from fundefs import *, which will throw all the functions into your current namespace. This is generally discouraged (and frowned upon) for larger modules as it will pollute your namespace, but for a few functions of your own this might just be what you're after. See also this very informative answer (and also comments therein).
as a middle ground through import very_long_and_descriptive_module_name as shorthand to access your functions as shorthand.fun1, shorthand.fun2 etc. (in the obvious case if your definitions are in the file very_long_and_descriptive_module_name.py)
You don't import User. What you want is to import ted. Typically, you would put ted.py in the same folder as your main python file, not in a separate folder.

setting $ENV{variable} when calling python [duplicate]

This question already has answers here:
How can I access environment variables in Python?
(16 answers)
Closed 9 years ago.
I'm new to Python and would like reproduce a convenience I used when working in Perl.
When calling a Perl script I usually set some $ENV variables (like VERBOSE, DEVELOP and DEBUG). Inside the called script I recover their values using
my $verbose=$ENV{VERBOSE};
my $develop=$ENV{DEVELOP};
my $debug=$ENV{DEBUG};
This allow print stmts conditional on these variables.
Can I do the same thing in Python? I know thanks to previous responses (Thank you!) to use os.environ[var] within the script to access the values. But I have not been able to figure out how to assign a value to a variable when I call the script from the command line as I could when callinbg a Perl script
Can values be set for such variables on the commandline invoking a python script?
TIA
They are accessible via the os.environ dictionary.
os.environ['VERBOSE']

memory impact and scope/lifespan of imported modules python [duplicate]

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

Categories