同时装了Python3和Python2,怎么用pip?
Ubuntu13.04, 系统内同时装了Python3.3 和 2.7
用sudo apt-get install python-pip
sudo apt-get install python3-pip
分别装了两个版本对应的pip
但是现在运行pip install 只能给2.7版安装库(好像是因为重名只能调用python-pip),怎么样设置才能给3.3版安装呢?
回复内容:
python3的用pip3 pip,pip2,pip3 对于Linuxpy -2 -m pip install sth,py -3 -m pip install sth 对于Windows 详见官方文档
https://docs.python.org/3/installing/ mac自带py2.7,自己安装了pip
想学习Python3,但是暂时又离不开Python2。在Windows上如何让它们共存呢?
目前国内网站经常会让大家把其中一个python.exe改个名字(嗯,我也这样讲过,在此纠正一下),这样区分开两个可执行文件的名字,但是这样做有一个重大的隐患,就是修改了名字的那个python对应的pip将无法使用。
官方的解法是什么?
事实上这个问题几年以前Python社区就给出了官方解决方案,只不过国内一直没有注意到罢了。
我们在安装Python3(>=3.3)时,Python的安装包实际上在系统中安装了一个启动器py.exe,默认放置在文件夹C:\Windows\下面。这个启动器允许我们指定使用Python2还是Python3来运行代码(当然前提是你已经成功安装了Python2和Python3)。
有关Python2和3的安装,可以参见Python部落(http://python.freelycode.com)-->影音学堂-->《0基础1小时,Python入门》的第一节。
如果你有一个Python文件叫 hello.py,那么你可以这样用Python2运行它
py -2 hello.py
类似的,如果你想用Python3运行它,就这样
py -3 hello.py
去掉参数 -2/-3
每次运行都要加入参数-2/-3还是比较麻烦,所以py.exe这个启动器允许你在代码中加入说明,表明这个文件应该是由python2解释运行,还是由python3解释运行。说明的方法是在代码文件的最开始加入一行
#! python2
或者
#! python3
分别表示该代码文件使用Python2或者Python3解释运行。这样,运行的时候你的命令就可以简化为
py hello.py
使用pip
当Python2和Python3同时存在于windows上时,它们对应的pip都叫pip.exe,所以不能够直接使用 pip install 命令来安装软件包。而是要使用启动器py.exe来指定pip的版本。命令如下:
py -2 -m pip install XXXX
-2 还是表示使用 Python2,-m pip 表示运行 pip 模块,也就是运行pip命令了。如果是为Python3安装软件,那么命令类似的变成
py -3 -m pip install XXXX
#! python2 和 # coding: utf-8 哪个写在前面?
对于Python2用户还有另外一个困惑,Python2要在代码文件顶部增加一行说明,才能够在代码中使用中文。如果指明使用的Python版本也需要在文件顶部增加一行,那哪一行应该放在第一行呢?
#! python2 需要放在第一行,编码说明可以放在第二行。所以文件开头应该类似于:
#! python2
# coding: utf-8
Copy after login
提供一个Windows下的解决方案:
先安好的python2的pip,它的默认路径是C:\Python27\Scripts

看到没有,它怕你找不到,自己给自己起了3个名字,随便你用哪个。
然后安装python3的pip,它的默认路径是C:\Users\***\AppData\Local\Programs\Python\Python35-32\Scripts
也是3个名字。

然后你把这两个路径填加到环境变量中,就可以愉快的用
pip2 install ... (python2)
pip3 install ... (python3)
分别安装库了。
为了回答这个问题,特意跑到一台 CentOS 6.5 上装了一个 Python3(手头没有 Ubuntu 的环境),然后又装了一个 pip,通过ln创建连接、alias创建别名这些操作,证明Python2 和 Python3 还是可以完美并存的。
题主的问题不在于能不能并存,而在于用默认的 pip install 命令给Python3 安装这个做法本身就是错误的,下面是我的解决方案:
环境上之前已经有了 Python 和 pip,
# python --version:
Python 2.7.9
# pip --version
pip 8.1.1 from /usr/local/lib/python2.7/site-packages (python 2.7)
Copy after login
同上,用 virtualenv
pip: www.pip-installer.orgvirtualenv: http://www.virtualenv.orgvirtualenvwrapper: http://virtualenvwrapper.readthedocs.org
virtualenv 是 lib 级别的隔离,如果需要python解释器的隔离,可以直接用
yyuu/pyenv · GitHub
建议用yyuu/pyenv · GitHub , 这样可以在不修改系统自带的python的前提下,安装任意多个Python版本,而且版本之间是相互隔离的,多版本管理起来很方便。
用 virtualenv 是王道。
另外根据 http://packages.ubuntu.com/zh-cn/quantal/all/python3-pip/filelist 这里看,python3-pip 装出来的应该是 pip-3.2 之类。用 dpkg -L python3-pip 看一下命令全路径。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

MySQL Workbench can connect to MariaDB, provided that the configuration is correct. First select "MariaDB" as the connector type. In the connection configuration, set HOST, PORT, USER, PASSWORD, and DATABASE correctly. When testing the connection, check that the MariaDB service is started, whether the username and password are correct, whether the port number is correct, whether the firewall allows connections, and whether the database exists. In advanced usage, use connection pooling technology to optimize performance. Common errors include insufficient permissions, network connection problems, etc. When debugging errors, carefully analyze error information and use debugging tools. Optimizing network configuration can improve performance

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

The MySQL connection may be due to the following reasons: MySQL service is not started, the firewall intercepts the connection, the port number is incorrect, the user name or password is incorrect, the listening address in my.cnf is improperly configured, etc. The troubleshooting steps include: 1. Check whether the MySQL service is running; 2. Adjust the firewall settings to allow MySQL to listen to port 3306; 3. Confirm that the port number is consistent with the actual port number; 4. Check whether the user name and password are correct; 5. Make sure the bind-address settings in my.cnf are correct.

As a data professional, you need to process large amounts of data from various sources. This can pose challenges to data management and analysis. Fortunately, two AWS services can help: AWS Glue and Amazon Athena.
