0

My problem is quite simple. I would like to install PyQuery using pip. So I write the following in the command line:

sudo pip install PyQuery

It said that PyQuery installed successfully. Then, if I write in the command line:

python3 test.py

It is said that it does not recognise the import PyQuery. However, if I replace python3 with python, this error message does not appear.

I suppose the problem is that pip installs modules for Python2 and not Python3, but I do not know how to change that.

Python2 version: 2.7 Python3 version: 3.4

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
Dust009
  • 315
  • 2
  • 10
  • 2
    try using `pip3` instead? Linux uses the first `pip` it finds, which is usually your system Python's pip (e.g. Python2). Python3's pip is symlinked to `pip3` in the same directory. โ€“ Adam Smith Oct 10 '15 at 18:41

1 Answers1

1

TL;DR

Each installed version of python have its own version of pip executable (at least for ubuntu), e.g. python2 have related pip2, python3 - pip3.

python and pip is just defaults which is simplinks to python2 or python3 binary.

So you can use

pip3 install <package> 

in most cases when you need to install package for python 3.

Lets find out how it works

pip is not binary!

Look at command for running pip python package, by executing following command:

$ less $(which pip)

Output will be something like this:

#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==1.5.4','console_scripts','pip'
__requires__ = 'pip==1.5.4'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.exit(
        load_entry_point('pip==1.5.4', 'console_scripts', 'pip')()
    )

Actually, most of commands in linux aren't binaries. A lot of them just helpful bindings, just like in case with pip.

$ ls -lah $(which pip)
-rwxr-xr-x 1 root root 281 ั‡ะตั€ 17 00:52 /usr/bin/pip

You can find simple description of how executable scripts works on linux. It is written for bash scripts but applied for any interpreted language: python, javascript, ruby, etc.

So, if pip is not binary, then what does this script?

RTFM about pip, in few words, pip is just a python module and can be executed as any other python module, look at PEP 0338, e.g.

$ python -m pip install <package>

have same same effect as for command

$ pip install <package>

Summary

pip is python script that runs pip package with passed parameters. Package relative for python version in $ which pip file. If you will open your pip script, you will find for which python version it is related for.

In my case it is:

$ head -1 $(which pip)
#!/usr/bin/python
$ /usr/bin/python -V
Python 2.7.6

or oneliner

$ $(expr substr `head -1 $(which pip)` 3 100) -V
Python 2.7.6
outoftime
  • 715
  • 7
  • 21