How to use pip3 for python 3.6 instead of python 3

2020-02-26 08:57发布

问题:

I'm using Kali dist so I have already installed Python 2.7, 3.5 and 3.6. Commands 'python' and 'pip' are associated with Python 2.7. But the 'python3' uses Python 3.6 while pip3 is installing packages for Python 3.5.
When I tried to create an venv:

pip3 -p python3.6 virtualenv myenv

I've got an error:

no such option: -p

How can I associate pip3 with Python 3.6 instead of Python 3.5?

回答1:

Your version of pip is inextricably linked to your version of Python, you cannot tell pip "use this Python" or "use that Python." If you have a version mismatch between pip3 (using Python 3.X) and python3 (being Python 3.Y), it means your problem is with multiple overlapping distributions of Python and a weirdly configured $PATH.

If you run pip3 --version it will tell you the site-packages directory and Python version number that pip3 is associated with.

If you run python3 and then execute >>> import site; site.getsitepackages(), it should print the site-packages directory your python3 is using.

If these do not match, you've got path problems and you'll need to post more information about what operating system you're on, what Python distributions you're using, and how you installed them.

Update/Summary of Comment Thread: Original poster had a distribution-bundled Python 3.6 installed alongside a self-installed Python 3.5. The pip3 on their path was associated with Python 3.6 (system Python), while the command python3 was associated with Python 3.5 (their self-installed Python). Resolution:

Run which -a python3 to find Python 3.5. Add the location of Python 3.5 to your $PATH. (Do it in .profile or .bash_profile to make it permanent.)



回答2:

You can explicitly run the pip3 script with a particular Python version, by prefixing it with the appropriate python3.x command:

ldo@theon:~> pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
ldo@theon:~> python3.5 $(which pip3) --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.5)


回答3:

When you install Python3, see if there's a comment such as this: Ignoring ensurepip failure: pip 9.0.1 requires SSL/TLS

You might see entries like this in the log:

INFO: Can't locate Tcl/Tk libs and/or headers

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _dbm                  _gdbm              
_lzma                 _sqlite3              _ssl               
_tkinter              readline                                 
To find the necessary bits, look in setup.py in detect_modules() for the     module's name.

The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
atexit                pwd                   time  

This answer describes using ensurepip https://stackoverflow.com/a/38250442/1607937

Also see this regarding openssl "SSL module in Python is not available" when installing package with pip3



回答4:

First find the right version of python you want to use:

$ which -a python3.6
/usr/bin/python3.6

then invoke that instance of python directly, e.g.

$ /usr/bin/python3.6 -m venv
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip] [--prompt PROMPT] ENV_DIR [ENV_DIR ...]
venv: error: the following arguments are required: ENV_DIR

Next, pip does not create virtual environments. The module venv does. Read the venv documentation for recommended usage. In your case, you might want:

$ /usr/bin/python3.6 -m venv myenv