How do I, in the main.py module (presumably), tell Python which interpreter to use? What I mean is: if I want a particular script to use version 3 of Python to interpret the entire program, how do I do that?
我如何在main.py模块(可能)中告诉Python使用哪个解释器?我的意思是:如果我想让一个特定的脚本使用Python的第3版来解释整个程序,我该怎么做?
Bonus: How would this affect a virtualenv? Am I right in thinking that if I create a virtualenv for my program and then tell it to use a different version of Python, then I may encounter some conflicts?
额外奖励:这对virtualenv有何影响?我是否正确地认为如果我为我的程序创建virtualenv然后告诉它使用不同版本的Python,那么我可能会遇到一些冲突?
5 个解决方案
#1
31
You can she-bang line the top line of the script but that'll only work when executing as ./my_program.py.
你可以直接在脚本的顶行排成行,但这只能在执行./my_program.py时起作用。
If you execute as python my_program.py
, then the whatever Python version that which python
returns will be used.
如果你执行python my_program.py,那么将使用python返回的任何Python版本。
In re: to virtualenv use: virtualenv -p /usr/bin/python3.2
or whatever to set it up to use that Python executable.
在re:to virtualenv中使用:virtualenv -p /usr/bin/python3.2或任何设置它以使用该Python可执行文件。
#2
27
Perhaps not exactly what you asked, but I find this to be useful to put at the start of my programs:
也许不完全是你问的问题,但我觉得这对于我的程序开头是有用的:
import sys
if sys.version_info[0] < 3:
raise Exception("Python 3 or a more recent version is required.")
#3
13
I would use the shebang #!/usr/bin/python
(first line of code) with the serial number of Python at the end ;)
我会使用shebang#!/ usr / bin / python(第一行代码)和最后的Python序列号;)
It is the same when you want to run Python from a Linux command line.
当您想从Linux命令行运行Python时,它也是一样的。
#4
9
While the OP may be working on a *nix platform this answer could help non-*nix platforms. I have not experienced the shebang approach work in Microsoft Windows.
虽然OP可能正在* nix平台上运行,但这个答案可以帮助非* nix平台。我没有在Microsoft Windows中体验过shebang方法。
Rephrased: The shebang line answers your question of "within my script" but I believe only for Unix-like platforms. Even though it is the Unix shell, outside the script, that actually interprets the shebang line to determine which version of Python interpreter to call. I am not sure, but I believe that solution does not solve the problem for Microsoft Windows platform users.
重新说明:shebang系列回答了“我的脚本内部”的问题,但我认为只适用于类Unix平台。即使它是在脚本之外的Unix shell,它实际上解释了shebang行以确定要调用哪个版本的Python解释器。我不确定,但我相信该解决方案无法解决Microsoft Windows平台用户的问题。
In the Microsoft Windows world, the simplify the way to run a specific Python version, without environment variables setup specifically for each specific version of Python installed, is just by prefixing the python.exe with the path you want to run it from, such as C:\Python25\python.exe mymodule.py or D:\Python27\python.exe mymodule.py
在Microsoft Windows世界中,简化运行特定Python版本的方式,而不是专门为每个特定版本的Python安装环境变量,只需在python.exe前面添加您想要运行它的路径,例如C:\ Python25 \ python.exe mymodule.py或D:\ Python27 \ python.exe mymodule.py
However you'd need to consider the PYTHONPATH and other PYTHON... environment variables that would point to the wrong version of Python libraries.
但是,您需要考虑PYTHONPATH和其他PYTHON ...环境变量,这些变量将指向错误版本的Python库。
For example, you might run: C:\Python2.5.2\python.exe mymodule
例如,您可以运行:C:\ Python2.5.2 \ python.exe mymodule
Yet, the environment variables may point to the wrong version as such:
然而,环境变量可能指向错误的版本:
PYTHONPATH = D:\Python27
PYTHONPATH = D:\ Python27
PYTHONLIB = D:\Python27\lib
PYTHONLIB = D:\ Python27 \ lib
Loads of horrible fun!
充满了可怕的乐趣!
So a non-virtualenv way, in Windows, would be to use a batch file that sets up the environment and calls a specific Python executable via prefixing the python.exe with the path it resides in. This way has additional details you'll have to manage though; such as using command line arguments for either of the "start" or "cmd.exe" command to "save and replace the "console" environment" if you want the console to stick around after the application exits.
因此,在Windows中,非虚拟化的方法是使用批处理文件来设置环境,并通过在python.exe前面添加它所在的路径来调用特定的Python可执行文件。这种方式还有其他详细信息。尽管如此;例如,如果您希望控制台在应用程序退出后保持不变,请使用“start”或“cmd.exe”命令的命令行参数来“保存并替换”控制台“环境”。
Your question leads me to believe you have several Python modules, each expecting a certain version of Python. This might be solvable "within" the script by having a launching module which uses the subprocess module. Instead of calling mymodule.py you would call a module that calls your module; perhaps launch_mymodule.py
您的问题让我相信您有几个Python模块,每个模块都期望某个版本的Python。通过使用使用子进程模块的启动模块,这可以在脚本内“解决”。您可以调用调用模块的模块,而不是调用mymodule.py;也许是launch_mymodule.py
launch_mymodule.py
import subprocess
if sys.argv[2] == '272':
env272 = {
'PYTHONPATH': 'blabla',
'PYTHONLIB': 'blabla', }
launch272 = subprocess.Popen('D:\\Python272\\python.exe mymodule.py', env=env272)
if sys.argv[1] == '252'
env252 = {
'PYTHONPATH': 'blabla',
'PYTHONLIB': 'blabla', }
launch252 = subprocess.Popen('C:\\Python252\\python.exe mymodule.py', env=env252)
I have not tested this.
我没有测试过这个。
#5
1
You can't do this within the Python program, because the shell decides which version to use if you a shebang line.
你不能在Python程序中这样做,因为如果你是一个shebang行,shell决定使用哪个版本。
If you aren't using a shell with a shebang line and just type python myprogram.py
it uses the default version unless you decide specifically which Python version when you type pythonXXX myprogram.py
which version to use.
如果您没有使用带有shebang行的shell并且只输入python myprogram.py它将使用默认版本,除非您在键入pythonXXX myprogram.py时使用哪个版本具体确定哪个Python版本。
Once your Python program is running you have already decided which Python executable to use to get the program running.
一旦你的Python程序运行,你已经决定使用哪个Python可执行文件来运行程序。
virtualenv
is for segregating python versions and environments, it specifically exists to eliminate conflicts.
virtualenv用于隔离python版本和环境,它专门用于消除冲突。
#1
31
You can she-bang line the top line of the script but that'll only work when executing as ./my_program.py.
你可以直接在脚本的顶行排成行,但这只能在执行./my_program.py时起作用。
If you execute as python my_program.py
, then the whatever Python version that which python
returns will be used.
如果你执行python my_program.py,那么将使用python返回的任何Python版本。
In re: to virtualenv use: virtualenv -p /usr/bin/python3.2
or whatever to set it up to use that Python executable.
在re:to virtualenv中使用:virtualenv -p /usr/bin/python3.2或任何设置它以使用该Python可执行文件。
#2
27
Perhaps not exactly what you asked, but I find this to be useful to put at the start of my programs:
也许不完全是你问的问题,但我觉得这对于我的程序开头是有用的:
import sys
if sys.version_info[0] < 3:
raise Exception("Python 3 or a more recent version is required.")
#3
13
I would use the shebang #!/usr/bin/python
(first line of code) with the serial number of Python at the end ;)
我会使用shebang#!/ usr / bin / python(第一行代码)和最后的Python序列号;)
It is the same when you want to run Python from a Linux command line.
当您想从Linux命令行运行Python时,它也是一样的。
#4
9
While the OP may be working on a *nix platform this answer could help non-*nix platforms. I have not experienced the shebang approach work in Microsoft Windows.
虽然OP可能正在* nix平台上运行,但这个答案可以帮助非* nix平台。我没有在Microsoft Windows中体验过shebang方法。
Rephrased: The shebang line answers your question of "within my script" but I believe only for Unix-like platforms. Even though it is the Unix shell, outside the script, that actually interprets the shebang line to determine which version of Python interpreter to call. I am not sure, but I believe that solution does not solve the problem for Microsoft Windows platform users.
重新说明:shebang系列回答了“我的脚本内部”的问题,但我认为只适用于类Unix平台。即使它是在脚本之外的Unix shell,它实际上解释了shebang行以确定要调用哪个版本的Python解释器。我不确定,但我相信该解决方案无法解决Microsoft Windows平台用户的问题。
In the Microsoft Windows world, the simplify the way to run a specific Python version, without environment variables setup specifically for each specific version of Python installed, is just by prefixing the python.exe with the path you want to run it from, such as C:\Python25\python.exe mymodule.py or D:\Python27\python.exe mymodule.py
在Microsoft Windows世界中,简化运行特定Python版本的方式,而不是专门为每个特定版本的Python安装环境变量,只需在python.exe前面添加您想要运行它的路径,例如C:\ Python25 \ python.exe mymodule.py或D:\ Python27 \ python.exe mymodule.py
However you'd need to consider the PYTHONPATH and other PYTHON... environment variables that would point to the wrong version of Python libraries.
但是,您需要考虑PYTHONPATH和其他PYTHON ...环境变量,这些变量将指向错误版本的Python库。
For example, you might run: C:\Python2.5.2\python.exe mymodule
例如,您可以运行:C:\ Python2.5.2 \ python.exe mymodule
Yet, the environment variables may point to the wrong version as such:
然而,环境变量可能指向错误的版本:
PYTHONPATH = D:\Python27
PYTHONPATH = D:\ Python27
PYTHONLIB = D:\Python27\lib
PYTHONLIB = D:\ Python27 \ lib
Loads of horrible fun!
充满了可怕的乐趣!
So a non-virtualenv way, in Windows, would be to use a batch file that sets up the environment and calls a specific Python executable via prefixing the python.exe with the path it resides in. This way has additional details you'll have to manage though; such as using command line arguments for either of the "start" or "cmd.exe" command to "save and replace the "console" environment" if you want the console to stick around after the application exits.
因此,在Windows中,非虚拟化的方法是使用批处理文件来设置环境,并通过在python.exe前面添加它所在的路径来调用特定的Python可执行文件。这种方式还有其他详细信息。尽管如此;例如,如果您希望控制台在应用程序退出后保持不变,请使用“start”或“cmd.exe”命令的命令行参数来“保存并替换”控制台“环境”。
Your question leads me to believe you have several Python modules, each expecting a certain version of Python. This might be solvable "within" the script by having a launching module which uses the subprocess module. Instead of calling mymodule.py you would call a module that calls your module; perhaps launch_mymodule.py
您的问题让我相信您有几个Python模块,每个模块都期望某个版本的Python。通过使用使用子进程模块的启动模块,这可以在脚本内“解决”。您可以调用调用模块的模块,而不是调用mymodule.py;也许是launch_mymodule.py
launch_mymodule.py
import subprocess
if sys.argv[2] == '272':
env272 = {
'PYTHONPATH': 'blabla',
'PYTHONLIB': 'blabla', }
launch272 = subprocess.Popen('D:\\Python272\\python.exe mymodule.py', env=env272)
if sys.argv[1] == '252'
env252 = {
'PYTHONPATH': 'blabla',
'PYTHONLIB': 'blabla', }
launch252 = subprocess.Popen('C:\\Python252\\python.exe mymodule.py', env=env252)
I have not tested this.
我没有测试过这个。
#5
1
You can't do this within the Python program, because the shell decides which version to use if you a shebang line.
你不能在Python程序中这样做,因为如果你是一个shebang行,shell决定使用哪个版本。
If you aren't using a shell with a shebang line and just type python myprogram.py
it uses the default version unless you decide specifically which Python version when you type pythonXXX myprogram.py
which version to use.
如果您没有使用带有shebang行的shell并且只输入python myprogram.py它将使用默认版本,除非您在键入pythonXXX myprogram.py时使用哪个版本具体确定哪个Python版本。
Once your Python program is running you have already decided which Python executable to use to get the program running.
一旦你的Python程序运行,你已经决定使用哪个Python可执行文件来运行程序。
virtualenv
is for segregating python versions and environments, it specifically exists to eliminate conflicts.
virtualenv用于隔离python版本和环境,它专门用于消除冲突。