如何分别为python3和python2设置不同的PYTHONPATH变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34066261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:25:40  来源:igfitidea点击:

how to set different PYTHONPATH variables for python3 and python2 respectively

pythonpythonpath

提问by Douglas Su

I want to add a specific library path only to python2. After adding export PYTHONPATH="/path/to/lib/"to my .bashrc, however, executing python3 gets the error: Your PYTHONPATH points to a site-packages dir for Python 2.x but you are running Python 3.x!

我只想向python2添加特定的库路径。但是,在添加export PYTHONPATH="/path/to/lib/"到 my.bashrc之后,执行 python3 得到错误:您的 PYTHONPATH 指向 Python 2.x 的站点包目录,但您正在运行 Python 3.x!

I think it is due to that python2 and python3 share the common PYTHONPATHvariable.

我认为这是由于 python2 和 python3 共享公共PYTHONPATH变量。

So, can I set different PYTHONPATHvariables respectively for python2 and python3. If not, how can I add a library path exclusively to a particular version of python?

那么,我可以PYTHONPATH分别为python2和python3设置不同的变量吗?如果没有,我如何才能将库路径专门添加到特定版本的 python?

采纳答案by Kenly

You can create a configuration file mymodule.pthunder lib/site-packages(on Windows) or lib/pythonX.Y/site-packages(on Unix and Macintosh), then add one line containing the directory to add to python path.

您可以mymodule.pthlib/site-packages(在 Windows 上)或lib/pythonX.Y/site-packages(在 Unix 和 Macintosh 上)下创建一个配置文件,然后添加一行包含要添加到 python 路径的目录。

From docs.python2and docs.python3:

docs.python2docs.python3

A path configuration file is a file whose name has the form name.pthand exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.pathmore than once. Blank lines and lines beginning with # are skipped. Lines starting with import(followed by space or tab) are executed.

路径配置文件是名称具有形式name.pth并存在于上述四个目录之一中的文件;它的内容是要添加到sys.path. 不存在的项目永远不会添加到sys.path,并且不会检查该项目是指目录而不是文件。不会sys.path多次添加任何项目。空行和以# 开头的行将被跳过。以import(后跟空格或制表符)开头的行被执行。

回答by KT.

PYTHONPATHis somewhat of a hack as far as package management is concerned. A "pretty" solution would be to packageyour library and installit.

PYTHONPATH就包管理而言,这有点像黑客。“漂亮”的解决方案是打包您的库并安装它。

This could sound more tricky than it is, so let me show you how it works.

这听起来可能比实际更棘手,所以让我向您展示它是如何工作的。

Let us assume your "package" has a single file named wow.pyand you keep it in /home/user/mylib/wow.py.

让我们假设您的“包”有一个名为的单个文件wow.py,并将其保存在/home/user/mylib/wow.py.

Create the file /home/user/mylib/setup.pywith the following content:

创建/home/user/mylib/setup.py包含以下内容的文件:

from setuptools import setup

setup(name="WowPackage",
      packages=["."],
)

That's it, now you can "properly install" your package into the Python distribution of your choice without the need to bother about PYTHONPATH. As far as "proper installation" is concerned, you have at least three options:

就是这样,现在您可以将您的软件包“正确安装”到您选择的 Python 发行版中,而无需担心PYTHONPATH. 就“正确安装”而言,您至少有三个选择:

  • "Really proper". Will copy your code to your python site-packages directory:

    $ python setup.py install
    
  • "Development". Will only add a link from the python site-packages to /home/user/mylib. This means that changes to code in your directory will have effect.

    $ python setup.py develop
    
  • "User". If you do not want to write to the system directories, you can install the package (either "properly" or "in development mode") to /home/user/.localdirectory, where Python will also find them on its own. For that, just add --userto the command.

    $ python setup.py install --user
    $ python setup.py develop --user
    
  • “真的很合适”。将您的代码复制到您的 python 站点包目录:

    $ python setup.py install
    
  • “发展”。只会添加从 python 站点包到/home/user/mylib. 这意味着对目录中代码的更改将会生效。

    $ python setup.py develop
    
  • “用户”。如果您不想写入系统目录,您可以将包(“正确”或“在开发模式下”)安装到/home/user/.local目录中,Python 也会在其中自行找到它们。为此,只需添加--user到命令中。

    $ python setup.py install --user
    $ python setup.py develop --user
    

To remove a package installed in development mode, do

要删除以开发模式安装的软件包,请执行

$ python setup.py develop -u

or

或者

$ python setup.py develop -u --user

To remove a package installed "properly", do

要删除“正确”安装的软件包,请执行

 $ pip uninstall WowPackage

If your package is more interesting than a single file (e.g. you have subdirectories and such), just list those in the packagesparameter of the setupfunction (you will need to list everything recursively, hence you'll use a helper function for larger libraries). Once you get a hang of it, make sure to read a more detailed manualas well.

如果您的包比单个文件更有趣(例如,您有子目录等),只需packagessetup函数的参数中列出那些(您需要递归列出所有内容,因此您将为更大的库使用辅助函数)。一旦你掌握了它,一定要阅读更详细的手册

In the end, go and contribute your package to PyPI -- it is as simple as calling python setup.py sdist register upload(you'll need a PyPI username, though).

最后,将您的包贡献给 PyPI —— 就像调用一样简单python setup.py sdist register upload(不过,您需要一个 PyPI 用户名)。

回答by Trevor Boyd Smith

I found that there is no way to modify PYTHONPATHthat is only for python2or only for python3. I had to use a .pthfile.

我发现没有办法修改PYTHONPATH仅适用于python2或仅适用于python3. 我不得不使用一个.pth文件

What I had to do was:

我必须做的是:

  • make sure directory is created in my home: $HOME/.local/lib/python${MAJOR_VERSION}.${MINOR_VERSION}/site-packages
  • create a .pthfile in that directory
  • test that your .pthfile is work
  • done
  • 确保在我的家中创建目录: $HOME/.local/lib/python${MAJOR_VERSION}.${MINOR_VERSION}/site-packages
  • .pth在该目录中创建一个文件
  • 测试您的.pth文件是否有效
  • 完毕

For more info on `.pth. file syntax and how they work please see: python2 docsand python3 docs.

有关`.pth 的更多信息。文件语法及其工作方式请参见:python2 docspython3 docs

(.pthfiles in a nutshell: when your python interpreter starts it will look in certain directories and see the .pthfile, open those files, parse the files, and add those directories to your sys.path(i.e. the same behavior as PYTHONPATH) and make any python modules located on those directories available for normal importing.)

.pth简而言之文件:当您的python解释器启动时,它将在某些目录中查找并查看.pth文件,打开这些文件,解析文件,并将这些目录添加到您的sys.path(即与 相同的行为PYTHONPATH)并使任何python模块位于那些可用于正常导入的目录。)

回答by BenSeedGangMu

If you don't want to bother with moving/adding documents in lib/site-packages, try adding two lines of code in the python2.7 script you would like to run (below.)

如果您不想在 中移动/添加文档lib/site-packages,请尝试在要运行的 python2.7 脚本中添加两行代码(如下)。

import sys
sys.path = [p for p in sys.path if p.startswith(r'C:\Python27')]

This way, PYTHONPATH will be updated (ignore all python3.x packages) every time you run your code.

这样,每次运行代码时都会更新 PYTHONPATH(忽略所有 python3.x 包)。