1

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.

MattDMo
  • 100,794
  • 21
  • 241
  • 231

2 Answers2

2

try :

python3.5 -m pip install setuptools
Loïc
  • 11,804
  • 1
  • 31
  • 49
  • No. I forgot to mention that, `pip` is not the problem, either; `setuptools` is already installed, such as `pygame`, `django`... The problem is that I can't make use of this modules with Python3, they only responds to Python 2.7. – Francisco Gómez García Aug 27 '16 at 21:53
  • If you used pip, then they probably are installed for python2.7, installing the modules within python3 helps making sure they actually are installed for the version of python you chose. Please give my answer a try. – Loïc Aug 27 '16 at 22:23
2

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

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • No. The problem persists. I always install packages with `pip3`, the `apt-get` method it was like my last resource... At this point, I don't know what is normal but when I _list_ pip packages they are all the same packages in `pip list` and `pip3 list`. Also, when I ran the command `pip -V` and `pip3 -V` the output was the same for **both** commands: `pip 8.1.2 from /usr/local/lib/python3.5/dist-packages (python 3.5)` That's what it makes me think the problem lies in the path. – Francisco Gómez García Aug 28 '16 at 06:30
  • @FranciscoGómez do you use Ubuntu or Debian? At least for Ubuntu, there should also be paths pointing to `/usr/lib`. I'm assuming you're using the system's Python 3 and not a manual install? I'll post my full path in a bit. – MattDMo Aug 28 '16 at 14:09
  • Could that be the problem? I installed it by compiling from source code. I'm using Ubuntu 16.04, too. One thing I just noticed when I compare our systems' path is that (in my case) `sys.path` points to: `/usr/local/lib/`, while the paths `/usr/lib/python3.5` _and_ `/usr/lib/python3/dist-packages` are not included... Adding this paths with the `append` method will fix the problema? – Francisco Gómez García Aug 28 '16 at 19:36
  • @FranciscoGómez is there a particular reason why you wanted to compile Python on your own? If you subscribe to `xenial-updates` in `apt`, things will get updated, including Python itself. I'm running the latest version right now from `/usr/bin/python` quite happily (and I like new stuff). You have a couple of options. You can upgrade Python (if necessary) using `apt`, then point it to `/usr/local/lib/python3.5/dist-packages` (it may already pick it up), or you can modify your handmade Python's `sys.path` to point to the `/usr/lib` locations. – MattDMo Aug 28 '16 at 19:41
  • @FranciscoGómez **make sure** you Google how to change `sys.path` and `*.pth` files, as beginning every program with `import sys; sys.path.append('/usr/lib/python3/...'` is dumb, and wasteful. – MattDMo Aug 28 '16 at 19:42
  • **You found the origin of my problems with Python3!** Everything works fine when I add: `/usr/local/lib/python3.5/dist-packages` to `sys.path`. The main reasons why I used to install Python compiling from source code are portability, the easy way to just move one file [to another system], compile and that's it... However, this has caused me a lot of troubles working with third-party modules. All I have to do now, is figure out how to make this changes permanent. So, finally, do you recommend me to always install Python3 using `apt-get`? I really want to be good Python programmer. – Francisco Gómez García Aug 28 '16 at 23:52
  • This has been a huge step to find a solution... **Thank you so much!** – Francisco Gómez García Aug 28 '16 at 23:53
  • @FranciscoGómez no problem, glad to help, and glad you got everything working. If you're using Ubuntu on all your systems, you can either use `apt` to install Python 3, or, in cases like the more recent versions, it'll already be installed, but you may need to add some extras, depending on your needs. For background info, start [here](https://wiki.ubuntu.com/Python) and read the links - basically the Debian/Ubuntu team(s) want to completely remove Py2 from the core distribution, and move it to `universe`, which is sort of the generic "third-party" set of packages. – MattDMo Aug 29 '16 at 18:04