This question already has answers here:
Calling the "source" command from subprocess.Popen
(9 answers)
Closed 5 years ago.
Hello I am trying to source of file say check.setup from my pythons script.
Code
import os
os.system("source /fullpathhere/check.setup")
It says command not found. Surprisingly I can source the same file directly from the shell.
check.setup is csh file. It is sourcing other .csh files and setting few environment variable I saw few answers here but no one could possibly solve the problem.
PS: I tried to write bash file instead of python. I also tried using subprocess.Popen. Problem persists.
Aashish
use popen
subprocess.Popen('source ./fullpathhere/check.setup', shell=True)
Why didn't tag your posting as csh, even though you explicitly mention csh in your question....
There is no executable namend source (you can verify it by typing bin/which source. Well, you pointed out by yourself, that this is supposed to be done by csh, but how should Python know that it needs to invoke csh, if you don't tell it?
I don't have a csh on my system, but if I remember right from the time a couple of decades ago, where I indeed used csh for programming, you can do something like
os.system("csh -c fullpathhere/check.setup")
Actually, I would also specify the -f flag for skipping .cshrc, unless you really need that to be sourced as well.
Related
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:
Closed 10 years ago.
Possible Duplicate:
Python subprocess wildcard usage
Using the Python 2.6 subprocess module, I need to run a command on a src.rpm file that I am building with a previous subprocess call.
Unfortunately, I am working with spec files that are not consistent, so I only have a vague idea of what the filename of the src.rpm should look like (for instance, I know the name of the package and the extension in something named "{package}-{version}.src.rpm" but not the version).
I do know, however, that I will only have one src.rpm file in the directory that I am looking, so I can call mock with a command like
mock {options} *.src.rpm
and have it work in shell, but subprocess doesn't seem to want to accept the expansion. I've tried using (shell=True) as an argument to subprocess.call() but even if it worked I would rather avoid it.
How do I get something like
subprocess.call("mock *.src.rpm".split())
to run?
Use the glob package:
import subprocess
from glob import glob
subprocess.call(["mock"] + glob("*.src.rpm"))
The wildcard * has to be interpreted by the SHELL. When you run subprocess.call, by default it doesn't load a shell, but you can give it shell=True as an argument:
subprocess.call("mock *.src.rpm".split(), shell=True)
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why do people write #!/usr/bin/env python on the first line of a Python script?
What does the line “#!/bin/sh” mean in a UNIX shell script?
It's a comment and should be ignored by the interpreter. Why is #!/usr/bin/python still in almost every program?
If the script is made executable, the operating system will use that first line to know which interpreter to run to parse the rest of the file to perform some action.
If you always run these kinds of scripts from the command line like python foo.py then you could remove the first line.
Not actually. It is tradition in Unix like operating systems to use the interpreter path given in the first line and use the path to that interpreter for interpreting the rest of the program. It is called as Shebang line. You would not cared for it if you were not on Unix like systems (linux. Mac OS, FreeBSD etc) but there are attempts in Python community to use similar shebang lines on windows too.
Read the Wikipedia article Shebang. Also remember outside the *nix world, the first shebang prefixed line is considered as a comment.