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

时间:2021-10-05 23:13:24

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添加一个特定的库路径。然后,将导出PYTHONPATH =“/ path / to / lib /”添加到我的.bashrc后,执行python3会收到错误:您的PYTHONPATH指向Python 2.x的site-packages目录,但您运行的是Python 3.x!

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

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

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

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

2 个解决方案

#1


17  

You can create a configuration file mymodule.pth under 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.

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

From docs.python2 and docs.python3:

来自docs.python2和docs.python3:

A path configuration file is a file whose name has the form name.pth and 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.path more 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开头的行(后跟空格或制表符)将被执行。

#2


20  

PYTHONPATH is somewhat of a hack as far as package management is concerned. A "pretty" solution would be to package your library and install it.

就包管理而言,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.py and you keep it in /home/user/mylib/wow.py.

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

Create the file /home/user/mylib/setup.py with 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 site-packages目录:

    $ 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 site-packages到/ home / user / mylib的链接。这意味着对目录中代码的更改将起作用。

    $ 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/.local directory, where Python will also find them on its own. For that, just add --user to the command.

    “用户”。如果您不想写入系统目录,可以将软件包(“正确”或“处于开发模式”)安装到/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 packages parameter of the setup function (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 manual as well.

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

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用户名)。

#1


17  

You can create a configuration file mymodule.pth under 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.

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

From docs.python2 and docs.python3:

来自docs.python2和docs.python3:

A path configuration file is a file whose name has the form name.pth and 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.path more 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开头的行(后跟空格或制表符)将被执行。

#2


20  

PYTHONPATH is somewhat of a hack as far as package management is concerned. A "pretty" solution would be to package your library and install it.

就包管理而言,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.py and you keep it in /home/user/mylib/wow.py.

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

Create the file /home/user/mylib/setup.py with 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 site-packages目录:

    $ 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 site-packages到/ home / user / mylib的链接。这意味着对目录中代码的更改将起作用。

    $ 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/.local directory, where Python will also find them on its own. For that, just add --user to the command.

    “用户”。如果您不想写入系统目录,可以将软件包(“正确”或“处于开发模式”)安装到/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 packages parameter of the setup function (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 manual as well.

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

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用户名)。