简体   繁体   English

使用 Python3 在 Python2 中安装包

[英]install packages in Python2 with Python3

I am trying to install numpy, nltk, etc packages for Python 2 to run a code.我正在尝试为 Python 2 安装 numpy、nltk 等软件包以运行代码。 But I have Python3 as well and the path variable is set to it.但我也有 Python3,并且路径变量设置为它。 When I try to use any pip install command it shows the package is available in Python3's directory.当我尝试使用任何 pip 安装命令时,它显示 package 在 Python3 的目录中可用。

Also, I am using VSCode, so I did not add the path variable.另外,我使用的是 VSCode,所以我没有添加路径变量。

I suggest you use virtual environments.我建议你使用虚拟环境。 Because if you read about virtual environments, you will find that they are created for such cases.因为如果您阅读有关虚拟环境的信息,您会发现它们是为这种情况而创建的。 To create virtual environments, you must do the following:要创建虚拟环境,您必须执行以下操作:

Make a note of the full file path to the custom version of Python you just installed.记下您刚刚安装的自定义版本 Python 的完整文件路径。

virtualenv -p /home/username/opt/python-2.7.15/bin/python venv

In order to use this environment's packages/resources in isolation, you need to “activate” it.为了单独使用这个环境的包/资源,你需要“激活”它。 To do this, just run the following:为此,只需运行以下命令:

source venv/bin/activate  (Linux)
./venv/Scripts/activate.bat (Windows)

Notice how your prompt is now prefixed with the name of your environment (venv, in our case).请注意您的提示现在是如何以您的环境名称为前缀的(在我们的例子中是 venv)。 This is the indicator that venv is currently active, which means the python executable will only use this environment's packages and settings.这是 venv 当前处于活动状态的指示器,这意味着 python 可执行文件将仅使用此环境的包和设置。

Now run the following:现在运行以下命令:

(venv) $ which python
/Users/ashkan/python-virtual-environments/venv/bin/python (in my case)

now you have access to python2.7.现在你可以访问python2.7了。

The best practice for this particular problem would be virtual environments.And for that matter Pipenv would be a good option.这个特定问题的最佳实践是虚拟环境。就此而言,Pipenv 将是一个不错的选择。

Install Pipenv.安装 Pipenv。

$ brew install pipenv (MacOs)
$ sudo apt install pipenv (Debian)
$ sudo dnf install pipenv  (Fedora)
 pip install pipenv  (Windows)

Creating virtual env with Pipenv.使用 Pipenv 创建虚拟环境。

pipenv install --python 2.7 numpy

This command will install create a virtual environment and install python 2.7(which will be used as the main interpreter once you activate the environment) along with numpy in that environment.此命令将安装创建虚拟环境并在该环境中安装 python 2.7(一旦您激活环境,将用作主解释器)以及 numpy。 This will avoid the packages version conflicts too.这也将避免包版本冲突。

To activate the environment激活环境

pipenv shell

If you are working in the Vs Code workspace then you should set the interpreter path(python path) to the path of the virtual environment.如果您在 Vs Code 工作区中工作,则应将解释器路径(python 路径)设置为虚拟环境的路径。

when we install anything using pip.当我们使用 pip 安装任何东西时。 it will install dependencies for default python version.它将安装默认 python 版本的依赖项。 so you can change the default python version using this link https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux因此您可以使用此链接更改默认 python 版本https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux

Hope this will solve your problem希望这能解决你的问题

After crating a virtual environment with python 2.7 you can install your required packages使用 python 2.7 创建虚拟环境后,您可以安装所需的软件包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM