This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Powershell Python: Change version used
I have both python 2.7 and 3.3 installed on my machine (windows). Say I want to switch back and forth between them; how do I write a script that will set change the path variable so I can switch back and forth easily without doing in manually?
import sys
sys.path.remove('<path to 2.7>')
sys.path.append('<path to 3.3>')
and vice-versa
Rather than change the path variable, it would probably be better to remove them both from your path and symlink the one you want to use in some common folder you have in your path (like /usr/local/bin). It would then be very simple to write a script to switch back and forth between them. Just remove the symlink and do ln -s again.
Edit: Disregard; see linked duplicate answer for a better solution.
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:
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.
This question already has answers here:
Change working directory in shell with a python script
(6 answers)
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 5 years ago.
I have the following piece of code:
import os
unixshell=os.environ["SHELL"]
dir="/home/user/somewhere"
if os.path.isdir(dir):
os.chdir(dir)
os.system(unixshell)
This is part of a script I wrote to bookmark folders I visit often in the terminal. A part of the script goes (cd's) to the bookmarked directory. I use it in the following way
~/olddir$ bk.py go [handle to bookmarked directory]
~/bookmarkeddir$
But, the thing I don't like is that a new bash shell is created, so when I need logout, I have to press CTRL+D multiple times.
The question is, can I change directory without creating a new shell? Is this possible using modules existent in python 2.4, or do I need to go to a higher version?
Edit: My question is more duplicate of this question:
Change directory of a parent process from a child process
The problem is that python runs in a child process, and it "can't" alter the current directory of the parent (I say "can't", but there are probably some debuggers that could do it - don't go there!).
The simplest solution is to use a function instead. For example:
bk() {
dir="/home/user/somewhere"
# equivalent to "if os.path.isdir(dir): os.chdir(dir)"
[[ -d $dir ]] && cd "$dir"
}
Put this into a file called bk.sh (for example).
To compile and load the function do:
. bk.sh
Then use bk whenever you want to change directory.
The difference with a function is that it runs in the current process, it does not create a new shell.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Test if executable exists in Python?
Is there a python function to let me detect whether a program is installed in the computer. I have a program that runs an .exe, that part works on windows but to run it in linux you need wine so I need a way for the python function to detect wine.
You can use function os.get_exec_path() to get a list of directories which are set in PATH environment variable. If the executable you are looking for is not present in any of these directories it is correct to assume that the program is not installed.
The code snipped to determine whether there is Wine installed then would look like this:
import os
winePath = None
for directory in os.get_exec_path():
testWinePath = os.path.join(directory, "wine")
if os.path.exists(testWinePath) and os.access(testWinePath, os.R_OK | os.X_OK):
winePath = executablePath
break
If there is Wine installed then the path to its executable file (wine) will be in winePath variable; if not found winePath will be None.
The code is also checking whether the file has correct perrmisions for reading and executing it.
The os.get_exec_path() is available since Python 3.2. In older versions you can use os.environ["PATH"].split(":") instead.
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.