Python:编码以考虑多个开发环境?
Python: coding to account for multiple development environments?
我在使用 Visual Studio 2015 和 PythonAnywhere 编写 Python 脚本之间来回切换。
虽然我可以在两个开发环境之间复制我的 .py 文件,但必须对 运行 同一脚本进行一些更改。例如,我在 VS 中到 Chromedriver 的路径可能是 C:/python27/libs/site-packages/... ,但是当我转到 PythonAnywhere 时,我想使用 Firefox,它的驱动程序在 ./drivers/ ... 两者之间的进口可能略有不同。
我什至不知道这是否可行,但是有没有办法在脚本的开头放置一些内容来告诉解释器:
if running this script on PythonAnywhere:
make these assumptions
if running this script on VisualStudio:
make these assumptions
...然后我就不用一直来回调整了?
如果我没有正确的术语来描述我正在尝试做的事情,我深表歉意。我看到有一个叫做 pyenv
的东西,但它似乎是 Python 2.x vs 3.x 项目的辅助工具(这不是我的问题。)
这里是 PythonAnywhere 开发者。可能有几种不同的方法可以做到这一点。这是我想到的第一个,这是基于您的 PC 具有 Windows 而 PythonAnywhere 处于 linux
的事实
# at the top of your scripts:
import sys
ON_PYTHONANYWHERE = sys.platform == "linux"
#... later in your code
if ON_PYTHONANYWHERE:
browser = webdriver.Firefox()
else:
browser = webdriver.Chrome()
我在使用 Visual Studio 2015 和 PythonAnywhere 编写 Python 脚本之间来回切换。
虽然我可以在两个开发环境之间复制我的 .py 文件,但必须对 运行 同一脚本进行一些更改。例如,我在 VS 中到 Chromedriver 的路径可能是 C:/python27/libs/site-packages/... ,但是当我转到 PythonAnywhere 时,我想使用 Firefox,它的驱动程序在 ./drivers/ ... 两者之间的进口可能略有不同。
我什至不知道这是否可行,但是有没有办法在脚本的开头放置一些内容来告诉解释器:
if running this script on PythonAnywhere:
make these assumptions
if running this script on VisualStudio:
make these assumptions
...然后我就不用一直来回调整了?
如果我没有正确的术语来描述我正在尝试做的事情,我深表歉意。我看到有一个叫做 pyenv
的东西,但它似乎是 Python 2.x vs 3.x 项目的辅助工具(这不是我的问题。)
这里是 PythonAnywhere 开发者。可能有几种不同的方法可以做到这一点。这是我想到的第一个,这是基于您的 PC 具有 Windows 而 PythonAnywhere 处于 linux
的事实# at the top of your scripts:
import sys
ON_PYTHONANYWHERE = sys.platform == "linux"
#... later in your code
if ON_PYTHONANYWHERE:
browser = webdriver.Firefox()
else:
browser = webdriver.Chrome()