简体   繁体   English

Linux上的两个版本的python。 如何将软件包安装到特定版本的python?

[英]Two versions of python on linux. How to install packages to specific version of python?

I am using Linux CentOs server. 我正在使用Linux CentOs服务器。 In my server two version of python were installed - 2.6 and 2.7. 在我的服务器上安装了两个版本的python-2.6和2.7。 Both path are entered in to path variable. 两条路径都输入到路径变量中。

I am trying to install pip using following command: 我正在尝试使用以下命令安装pip:

> sudo yum install python-pip 

But it installed to python2.6 ( /usr/bin/python ). 但是它安装到python2.6( /usr/bin/python )。 So If I am trying to install package to python using pip it goes to python2.6. 因此,如果我尝试使用pip将软件包安装到python,它将转到python2.6。 For example 例如

> pip install XlsxWriter

First of all I want to install pip to python2.7 version and then have to some packages. 首先,我想将pip安装到python2.7版本,然后再安装一些软件包。 Path of python2.7 is /usr/local/bin/python2.7 . python2.7的路径是/usr/local/bin/python2.7 How do I manage packages for different versions of python? 如何管理不同版本的python的软件包? (I don't want to change my default python version (2.6).) (我不想更改默认的python版本(2.6)。)

My second question is... My project needs 8 python packages. 我的第二个问题是...我的项目需要8个python软件包。 How do I install all packages in a single command? 如何在单个命令中安装所有软件包? Is there any concept like package.json (which is used for nodejs) or composer.json (which is used for php) in python? python中是否有像package.json(用于nodejs)或composer.json(用于php)这样的概念? I heard about setup.py but I could not clear about that. 我听说过setup.py,但我不清楚。 Is this setup.py concept similar to package.json? 这个setup.py概念类似于package.json吗?

If you want to have control over what Python version you're using, I'd suggest considering virtual environments . 如果您想控制使用的Python版本,建议您考虑使用虚拟环境

You can then create a stand-alone virtual environment for each project and use the Python version you need. 然后,您可以为每个项目创建一个独立的虚拟环境,并使用所需的Python版本。

This will create a virtual environment for your project with Python 2.7, install <package> and deactivate the virtual environment. 这将使用Python 2.7为您的项目创建一个虚拟环境,安装<package>并停用该虚拟环境。

cd my_project_folder
virtualenv -p /usr/bin/python2.7 venv
source venv/bin/activate
pip install <package>
deactivate

As for your second question, I recommend using pip freeze and pip install -r ... 至于第二个问题,我建议使用pip freezepip install -r ...

First, install all the packages you need: 首先,安装所需的所有软件包:

source venv/bin/activate
pip install <package1>, <package2>,...

Then save the list of packages with their current versions: 然后保存软件包列表及其当前版本:

pip freeze > requirements.txt

This will create a requirements.txt file that you can distribute with the project. 这将创建您可以随项目一起分发的requirements.txt文件。 When somebody wants to install all the dependencies, they will need to do: 当有人要安装所有依赖项时,他们将需要执行以下操作:

pip install -r requirements.txt

For Python 3, virtual environments are supported by default - venv . 对于Python 3,默认情况下支持虚拟环境-venv

Say you have two python versions installed 2.7 and 3.5, 假设您安装了两个python版本2.7和3.5,

Set the Environment variables 设置环境变量

  • set python2 as the env variable for python2.7 python2设置为python2.7的env变量
  • set python as evn variable for python3.5 python设置为python3.5的evn变量

Install packages for python2: python2 -m pip install < package-name > 安装python2的软件包: python2 -m pip install <软件包名称>

Install packages for python3: python -m pip install < package-name > or pip install < package-name> 为python3安装软件包: python -m pip install <软件包名称>或pip install <软件包名称>

Try which pip , choose your pip 尝试which pip ,选择您的点

then simply do sudo /path_to_your_version_of_pip/pip install your module 然后只需执行sudo /path_to_your_version_of_pip/pip install您的模块

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

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