I use both python 2 and python 3 on my machine. I take advantage of the she-bang notation at the top of my python scripts and use the new python launcher that came with python 3.
I would like to locate the python 2 path for python.exe from within a python 3 script running on windows.
sys.executable and similar commands won't work because it shows the path to the currently running python. It will not show the path to another version of python.
How about the following? It utilizes the new launcher to execute python2, and gives it a one-liner to print sys.executable, so it's the python2 interpreter running it.
import subprocess
print(str(subprocess.run(['py', '-2', '-c', 'import sys;print(sys.executable)'],
stdout=subprocess.PIPE).stdout, 'utf-8'))
Output:
$ python -V
Python 3.6.5
$ python get_two.py
C:\Python27\python.exe
Related
I have both Python 3 (C:\Python\Python38\python.exe) and Python 2 (C:\Python\Python27\python.exe) installed on my Windows 10 computer, and I want to use Python 2 for a project in VSCode.
However, when I select my interpreter to Python 2.7.1 32--bit and run my code, it still runs in Python 3. Typing python -V in PowerShell or Python window gives me "Python 3.8.2". Both versions have been added to my PATH variables, so I am not quite sure why selecting Python 2 in the VSCode interpreter menu still gives me Python 3.
Has anyone encountered anything similar?
Can you add print(sys.prefix) to check which python interpreter you are using? The python --version you get in the VSCode is not the python interpreter you are using. Python2 works well in my VSCode.
And if you want to distinguish the python2 and python3 clearly. You can rename the python.exe to python2.exe which under the Python2 installation Folder. Then in the Powershell, you can get python3 through the python command and python2 through the python2 command.
I have a script which won't run inside of Nuke's built-in Python interpreter, so I'm trying to launch in via the system's Python instead. I'm using subprocess.Popen to do this, but it still won't run inside a subprocess (missing modules), even though it runs fine at the command prompt (cmd.exe)
I think the problem is, that I just don't understand what environment my subprocess starts up in. It doesn't even run the expected version of Python.
In cmd.exe:
C:/Python27/python.exe -V
> Python 2.7.16
In Nuke:
import subprocess as sub
print sub.Popen("C:/Python27/python.exe -V", stderr=sub.PIPE).communicate()[1]
# Result: Python 2.7.13
print sub.Popen('C:/Python27/python.exe -c
"import sys;print(sys.executable)"',
stdout=sub.PIPE).communicate()[0]
# Result: C:\Python27\python.exe
Where is this lower version of Python coming from? It's probably not a coincidence that Nuke's built-in Python is also 2.7.13, but why would Popen run that and not the .exe I'm specifying?
Note: same result whether I give shell=True or shell=False
I am trying to run a python program called compare.py with the linux nohup command which keeps the program running until it is done without interruption. my python program has packages which can only run on python 2.7 and when i use nohup command program is run in python 2.6. how do i change the version of python when using nohup?
Example: nohup python compare.py $
I tried doing:
alias python=python2.7
before starting program and version of python isn't switched. how do i switch the version of python to 2.7 when i run nohup?
The easiest way would be to use a shebang line to specify the interpretter. At the start of your Python file, put something like
#!/usr/bin/python2.7
# This should be a path to an interpreter that you know for sure is Python 2.7
Then, use chmod +x file.py to make the Python file itself executable, and omit the python part of your nohup command, eg. nohup ./compare.py.
I had the same issue with anaconda python. While using nohup python, it was using python 2.7 but generic python command in terminal was taking me to 3.6.
nohup ~/anaconda3/bin/python scriptname.py
Providing full path to python after nohup command will solve the issue
I have a machine running OSX Yosemite (it has been through several versions of OSX, which may make a difference).
I noticed an anomily with whether python could import libraries depending on whether the script was run directly, i.e.
./Myscript.py
Or by expressly calling python
python Myscript.py
Now, if I type
$whereis python
/usr/bin/python
And my shebang line in the script is
#!/usr/bin/python
So I assumed that the same version of python was running in both cases.
But after investigating I find
$python --version
Python 2.7.6
$/usr/bin/python --version
Python 2.7.10
So it would seem that the python being executed is not the one I get when I do a whereis
Can anyone please shed some light on this, and also clarify how to fix it? I really want to be running 2.7.10 in both cases, since right now when I install libraries they go into 2.7.6, but when I run scripts, they run 2.7.10 and can't see the libraries.
Thanks
Jon
Don't use whereis, that command ignores your PATH environment variable. From the manpage:
The whereis utility checks the standard binary directories for the specified programs, printing out the paths of any it finds.
Emphasis mine.
You have a PATH environment variable that includes a 'nonstandard' binary directory. Use which to find where python comes from:
$ which python
which gives you the actual binary used for your current shell configuration:
The which utility takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked.
You could use which -a to find all possible completions for the command:
$ which -a python
Also see “whereis” and “which” return different paths in Mac OS X on Super User.
Demo:
$ PATH=/opt/homebrew/bin:$PATH whereis python
/usr/bin/python
$ PATH=/opt/homebrew/bin:$PATH which -a python
/opt/homebrew/bin/python
/usr/bin/python
So even with PATH explicitly pointing to my homebrew directory, whereis ignores it. which finds it and lists it first (the -a argument made it look for more options).
In my terminal and in CodeRunner my Python is updated to 2.7.6 but when I ran a shell script in the OSX Automator I found that it is running 2.7.2
How can I update the Automator Python to 2.7.6 like the rest of my compilers ?
I couldn't specify explicitly which python for it to use.
So, I ran it in bash environment with following command:
$ your/python/path /path/to/your/python/script.py
And make sure first line of your python program contains the path to the python environment you wish to use.
Eg:
#! /usr/local/bin/python