This question already has answers here:
python refresh/reload
(7 answers)
How do I unload (reload) a Python module?
(22 answers)
Closed 8 years ago.
I ran my script from Python environment launched in bash:
>>> import myscript
I then modified my script a little and save it. Then run again
>>> import myscript
But it doesn't run the updated script.
How can I tell Python to run the updated one? Thanks!
Simply reload it like this
reload(myscript)
Quoting from the docs,
Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter.
reload builtin is what you actually need:
https://docs.python.org/2/library/functions.html#reload
Related
This question already has answers here:
How to set environment variables in Python?
(19 answers)
Running 'export' command with Pythons 'subprocess' does not work
(1 answer)
Closed 2 years ago.
I want to run setenv command through python script, the linux command is as below
setenv MODEL path/to/the/model
I tried using os.environ(), but I couldn't get the right syntax.
note - I'm using python 2.7
os.environ isn't a function. Check out the docs: https://docs.python.org/2/library/os.html#os.environ
This question may also be helpful to you: How to set environment variables in Python. If it is, pay attention to the comments on the top answer.
This question already has answers here:
Multiple Python versions on the same machine?
(11 answers)
Closed 3 years ago.
while there are multiple versions of Python installed on a Linux machine, is there a way to mention in the script to be open with a specific version, say, 3.8 even if we issue the #python script.py as opposed to python3.8 script.py ?
I don't want to use Linux alias command. I wanna know if that is possible to be accomplished within the script
Use shebang. #!/usr/bin/python
In the first line of the code with the serial number of Python you want at the end.
You'll need to then run your script like ./my_script.py
You can select the interpreter by adding
#!/usr/bin/env pythonXX
as the first line in your script, provided the version is in the path.
You can also invoke the interpreter directly with the script as the argument.
pythonXX script.py
Depending on your situation
pythonXX -E script.py
might be better. That way the interpreter ignores paths set by environmental variables.
For more details view
https://docs.python.org/3/using/cmdline.html
This question already has answers here:
How to quickly debug misbehaving script in Python without pdb?
(3 answers)
Closed 9 years ago.
I have a high level logic error deep within my Python script, and pdb doesn't help to debug it. Is there any other way to see what is being executed after I run my script?
NOTE: pdb is too slow and inconvenient for me. I wish I could grep over all cases when my function is executed, instead of inspecting manually each and every call, set/unset breakpoints. The state is lost when I exit pdb and its user interface is more confusing than helpful - requires docs at hand.
I found a way to do this using excellent trace module that comes with Python.
An example how to troubleshoot module installation problem:
python -m trace -t setup.py install > execution.log
This will dump all source line of setup.py install execution to execution.log. I found this to be more useful than pdb approach.
This question already has answers here:
Auto-load a module on python startup
(5 answers)
Closed 9 years ago.
I'm using python on Windows. And I'm trying to find a way to import some default module when starting python. It means, when starting python, some modules should be already imported, just like builtins. Is there any way?
Thanks.
If you mean while going into the interactive mode, just make a small startup script. Instead of just launching python with:
python
try instead:
python -i startupImports.py
startupImports.py:
import time
from collections import defaultdict
from customclass import myclass
#...etc
Define a PYTHONSTARTUP environment variable and point it to a python file and it will be loaded when you start python interactively without a command line script.
I have my PYTHONSTARTUP point to a .pythonrc where I try to import a __boot__.py (bootstrap) in the current directory for any interactive python session.
try:
from __boot__ import *
except Exception:
pass
There's a bunch of other things in my pythonrc like __future__ imports, readline setup, etc.
One option is adding your import to sitecustomize.py. This file is automatically imported at startup. This will be loaded whether Python is started in interactive mode or not. To quote the docs:
[In site.py] an attempt is made to import a module named sitecustomize, which can perform arbitrary site-specific customizations. It is typically created by a system administrator in the site-packages directory. If this import fails with an ImportError exception, it is silently ignored.
Another option is to set the PYTHONSTARTUP environment variable to a startup script with your import. This will only be executed if Python is started in interactive mode, though.
This question already has answers here:
Passing options to Python executable in non-interactive mode
(4 answers)
Closed 7 years ago.
I would like to execute a script work.py in Python, after executing some initialization script init.py.
If I were looking for an interactive session, executing python -i init.py or setting PYTHONSTARTUP=/path/to/init.py would do the trick, but I am looking to execute another script.
Since this is a generic case which occurs often (init.py sets environment and so is the same all of the time), I would highly prefer not referencing init.py from work.py. How could this be done? Would anything change if I needed this from a script instead of from the prompt?
Thank you very much.
More generally than in the accepted answer of C0deH4cker, this is discussed in the Python manual in Section 2.2.5 - Cusomization Modules. The basic idea is, to get the location of the special start-up script, one needs to execute the following Python code, e.g. from the interactive session of the interpreter:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.2/site-packages'
The output should be exactly such a location, in the file sitecustomize.py.
Python has a special script that is run on startup. On my platform it is located at /usr/lib/python2.5/site-packages/sitecustomize.py IIRC. So, you could either put init.py in that directory alongside a sitecustomize.py script that imports it, or just paste the content of init.py in the sitecustomize.py.