Python3 always shows ImportError message

Whenever I try to run a script the python interpreter always shows an ImportError message such as (e.g.) No module named 'setuptools'. So, I tried install (or to satisfy this requirement) with apt-get... I do this for both Python 2.7 and Python 3.5 until Requirement already satisfied.

First of all, I don't work with Python 2.7, but it's the defaul version for the interpreter. So, how could I solve this problem to work with Python 3.5? I tried this:

>>> import sys
>>> print(sys.path)
['',
 '/usr/local/lib/python35.zip',
 '/usr/local/lib/python3.5',
 '/usr/local/lib/python3.5/plat-linux',
 '/usr/local/lib/python3.5/lib-dynload',
 '/usr/local/lib/python3.5/site-packages']

This was for Python3, for Python2 I did the same to compare the paths and I got this:

>>> import sys
>>> print(sys.path)
['',
 '/usr/local/lib/python2.7/dist-packages/pygame-1.9.2b8-py2.7-linux-x86_64.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0']

Now... Should it work if I use the append() method to add all the paths of Python2 to the paths in Python3? Also, I've been considered to completely uninstall Python2, but I know this will cause more problems in my system that the one I try to solve.

Upvotes: 1

Views: 1489

Answers (2)

MattDMo
MattDMo

Reputation: 102892

From your description of the problem, you have likely been installing the Python 2 versions of the packages you want with apt and/or pip. sudo apt-get install python-django, for example, will install the Python 2 version of Django, while sudo apt-get install python3-django installs the Py3 version.

You will eventually run into a situation where you need to use pip, as a package you want won't be in the Debian/Ubuntu repositories. In that case, make sure you're using the right pip. Try running

pip -V

and

pip3 -V

to see which Python versions are attached when you call pip, then use the appropriate one for the version of Python you wish to target.

Finally, under no circumstances whatsoever should you add the Python 2 paths to Python 3's sys.path.


EDIT

Here is my sys.path on Ubuntu 16.04 using the system's Python 3.5.2:

$ python3
Python 3.5.2 (default, Jul  5 2016, 12:43:10) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> from pprint import pprint as pp
>>> pp(sys.path)
['',
 '/usr/local/lib/python3.5/dist-packages/pandas-0.18.1-py3.5-linux-x86_64.egg',
 '/usr/local/lib/python3.5/dist-packages/github3.py-1.0.0a4-py3.5.egg',
 '/usr/local/lib/python3.5/dist-packages/uritemplate.py-0.3.0-py3.5.egg',
 '/usr/lib/python3/dist-packages',
 '/usr/lib/python35.zip',
 '/usr/lib/python3.5',
 '/usr/lib/python3.5/plat-x86_64-linux-gnu',
 '/usr/lib/python3.5/lib-dynload',
 '/usr/local/lib/python3.5/dist-packages']
>>> print(sys.executable)
/usr/bin/python3
>>>

You'll notice that the paths are dist-packages paths, while you have site-packages. From a user's perspective, there's very little difference between the two, so don't worry about it. I also changed some of my paths on purpose (it's a long story).

Upvotes: 2

Loïc
Loïc

Reputation: 11942

try :

python3.5 -m pip install setuptools

Upvotes: 2

Related Questions