17

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?

  • Possible duplicate of [How to run pip of different version of python using python command?](https://stackoverflow.com/questions/34803040/how-to-run-pip-of-different-version-of-python-using-python-command) – phd Oct 19 '17 at 11:57
  • The '-p python3.6' should be the arguments for virtualenv not pip3 – jgritty Oct 19 '17 at 20:23
  • 1
    If one of these answers solved your problem, please mark it. – John Schmitt Oct 22 '17 at 00:19
  • 1
    Also see [this question](https://unix.stackexchange.com/questions/399626/why-is-kali-linux-so-hard-to-set-up-why-wont-people-help-me/399627#399627) and the advice given there: ***If you aren't already a Linux pro, don't use Kali.*** This cannot be stressed enough. – charlesreid1 Oct 22 '17 at 19:15

7 Answers7

21

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.)

charlesreid1
  • 4,360
  • 4
  • 30
  • 52
  • 1
    `pip3 --version` `pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.5)` `>>> import site; site.getsitepackages()` `['/usr/local/lib/python3.6/site-packages']` –  Oct 19 '17 at 09:29
  • Yep, your pip3 and python binaries are mismatched. How and why do you have two versions of Python 3 installed? You should uninstall both versions of Python 3 and re-install one or the other. Please update your post with information about how you installed these two versions of Python so I can help you figure out how to uninstall them. – charlesreid1 Oct 19 '17 at 10:50
  • I don't remember exactly but I think Python 3.6 was built in dist and Python 3.5 I've installed by myself. I think so, because I read that the built-in versions do not have pip –  Oct 19 '17 at 16:50
  • 4
    It is quite possible on Linux systems, and not problematic, to have multiple versions of Python installed. I currently have versions from 3.2 up to 3.6 on my main Debian machine. This is handy for testing compatibility of code that I write. – Lawrence D'Oliveiro Oct 19 '17 at 18:07
  • @LawrenceD'Oliveiro Sure, and if you know what you're doing and need python 3.5 alongside 3.6 (*and have a strategy for managing inevitable $PATH and $PYTHONPATH conflicts*) that's fine. The OP clearly **does not** need 3.5 and 3.6 side by side. – charlesreid1 Oct 19 '17 at 20:42
  • @Lorkes Then the problem is with your `$PATH` environmental variable. That's where your system is looking for versions of Python when you type "python" at the command prompt. The quick fix is to find the path to where python 3.5 is located on your machine, and add it to the front of your path. Running `which -a python3` (NOT python3.6, as advised above) will tell you where python 3.5 is located. Please post the output of this command: `which -a python3`. – charlesreid1 Oct 19 '17 at 20:46
  • (My *real* and *quick* advice here, again, is to uninstall Python 3.5 and use the system Python. It's pretty obvious from your choice of distro that you just need a python3 to hack away with. But if you *really* want to keep both, just post output of `which -a python3` so we can figure out what to add to your `$PATH`.) – charlesreid1 Oct 19 '17 at 20:53
  • `which -a python3` `/usr/local/bin/python3 /usr/bin/python3 ` –  Oct 20 '17 at 08:06
  • Looks like I made a mistake. When I created a new user (5 mins ago), Python 3.5 and Python 3.6 were already installed. And pip3 is connected to Python 3.5 –  Oct 20 '17 at 08:45
  • Well... alrighty then. Please mark an answer as accepted so that others know what answer/information was useful. Also see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – charlesreid1 Oct 22 '17 at 19:17
  • @charlesreid1 This answer is almost wrong. There is no problem having multiple Python versions installed. Just invoke `pip` via the Python you want to install packages for as in `python3.9 -m pip ...`. – BlackJack Oct 07 '20 at 10:15
8

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)
Lawrence D'Oliveiro
  • 2,768
  • 1
  • 15
  • 13
  • 5
    `pip3 -V pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.5) ` `python3.6 $(which pip3) --version` `Traceback (most recent call last):` `File "/usr/bin/pip3", line 9, in ` `from pip import main` `ModuleNotFoundError: No module named 'pip' ` –  Oct 19 '17 at 09:35
4

To install a package in the same version location that's associated with the version associated with python3, use the following:

python3 -m pip install [package]

to pick a specific version that you'd like your package to be associated with (so you're not guessing with the above):

python3.5 -m pip install [package]
python3.7 -m pip install [package]

Also, be careful because pip3 can point to different locations and may not necessarily match the location of the python3 binary. I just found that out when I did a pip3 install and it failed to import when running python3.

You can also explicitly call pip3.5, pip3.7, etc, but honestly I prefer using the python[version] -m pip install [package] method because I know that it will install the package in the location associated with whatever python3.x binary I'm using.

Mike
  • 504
  • 7
  • 23
0

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

Den-Jason
  • 2,395
  • 1
  • 22
  • 17
0

If you want to use only one version of python you sould probably create an alias. Add this line at the end of your ~/.bashrc flie:

alias pip='python3.6 -m pip'

Then, run source ~/.bashrc, and now pip --version will show something like:

pip xx.x.x from /usr/lib/python3/dist-packages/pip (python 3.6)
Leonardo Rick
  • 680
  • 1
  • 7
  • 14
0

you can update line #1 from /usr/bin/pip3 to #!/usr/bin/python3.8 as below

#!/usr/bin/python3.8
# GENERATED BY DEBIAN

import sys

# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.
from pip import main
if __name__ == '__main__':
    sys.exit(main())
-1

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
John Schmitt
  • 1,148
  • 17
  • 39