如何在 virtualenv 中 运行 一个 python 脚本
How to run a python script in virtualenv
我在“/home/user1/public_html/cgi-bin”中有一个 hello.py 文件,显示 python 版本:
#!/usr/bin/python
import platform
print "Content-type: text/html\n\n"
print(platform.python_version())
当我转到 url 脚本时,它在浏览器中显示“2.6.6”。
我想使用 python 3.5,所以我让虚拟主机在“/home/user1/virtualenv/testproject/”
中安装了一个 virtualenv
当我尝试将代码的第一行更改为:
#!/home/user1/virtualenv/testproject/3.5/bin/python
import platform
print "Content-type: text/html\n\n"
print(platform.python_version())
脚本将显示 "It works! Python 3.5.5",这不是我在脚本中编码的内容。
我是否应该将 hello.py 文件放入 virtualenv 目录?还是我的 python 3.5 路径不正确?或者我只是完全误解了所有这些是如何工作的?任何帮助将不胜感激。
这一定能解决您的问题
您的代码是正确的,不用担心,因为它会在解释器中给出正确的输出。
发生此问题是因为您在计算机上安装了两个版本的 python,并且默认情况下 python 2 的路径设置在环境变量的优先级中。
脚本在解释器中给出了正确的输出,因为解释器不需要路径优先级,但是当您从浏览器或命令提示符或控制台 运行 它时,需要路径并搜索 python 从系统环境 variable.In 你的情况,首先它得到 python 2 的路径,以便它显示 python 2
的版本
他们将为您的问题提供两种解决方案,您可以做任何人。
从您的系统中卸载 python 2。这将立即解决您的问题。
如果你想保留python的两个版本,那么为了解决这个问题,你应该在环境变量的优先级中设置python3的路径。像这样
看截图,Path of Python 2 is Upper in the Environment Variable should be below the Path of Python 3
像这样
这是设置优先路径的正确静态方法
谢谢,期待这将解决您的问题
phd 的评论是正确答案。 print
是Python3中的一个函数,所以你必须调用它:print("Content-type: text/html\n\n")
。 print-as-operator 是 Python 3.
中的 SyntaxError
我在“/home/user1/public_html/cgi-bin”中有一个 hello.py 文件,显示 python 版本:
#!/usr/bin/python
import platform
print "Content-type: text/html\n\n"
print(platform.python_version())
当我转到 url 脚本时,它在浏览器中显示“2.6.6”。
我想使用 python 3.5,所以我让虚拟主机在“/home/user1/virtualenv/testproject/”
中安装了一个 virtualenv当我尝试将代码的第一行更改为:
#!/home/user1/virtualenv/testproject/3.5/bin/python
import platform
print "Content-type: text/html\n\n"
print(platform.python_version())
脚本将显示 "It works! Python 3.5.5",这不是我在脚本中编码的内容。
我是否应该将 hello.py 文件放入 virtualenv 目录?还是我的 python 3.5 路径不正确?或者我只是完全误解了所有这些是如何工作的?任何帮助将不胜感激。
这一定能解决您的问题
您的代码是正确的,不用担心,因为它会在解释器中给出正确的输出。
发生此问题是因为您在计算机上安装了两个版本的 python,并且默认情况下 python 2 的路径设置在环境变量的优先级中。
脚本在解释器中给出了正确的输出,因为解释器不需要路径优先级,但是当您从浏览器或命令提示符或控制台 运行 它时,需要路径并搜索 python 从系统环境 variable.In 你的情况,首先它得到 python 2 的路径,以便它显示 python 2
的版本他们将为您的问题提供两种解决方案,您可以做任何人。
从您的系统中卸载 python 2。这将立即解决您的问题。
如果你想保留python的两个版本,那么为了解决这个问题,你应该在环境变量的优先级中设置python3的路径。像这样
看截图,Path of Python 2 is Upper in the Environment Variable should be below the Path of Python 3
像这样
这是设置优先路径的正确静态方法
phd 的评论是正确答案。 print
是Python3中的一个函数,所以你必须调用它:print("Content-type: text/html\n\n")
。 print-as-operator 是 Python 3.