Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 has no acces to python2 modules (ubuntu)

Im fairly new to programming and Ubuntu. Yesterday I finally managed to create a dual-boot system, so now I'm running Ubuntu 12.04 LTS. For a school project, I need to work in Python3 with a module called SPARQLWrapper (https://pypi.python.org/pypi/SPARQLWrapper).

On my freshly installed Ubuntu, I've installed the latest version of Python. When I type "python3" in my terminal, python 3.2.3 starts so thats good. I installed easy_install (sudo apt-get install python-setuptools), and downloaded and installed the SPARQLWrapper egg file (sudo easy_install SPARQLWrapper-1.5.2-py3.2).

If I run python2 and use "import SPARQLWrapper", it just works. But if I try the same in python3 it gives me the following error:

x@ubuntu:~$ python3
Python 3.2.3 (default, Oct 19 2012, 20:10:41) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import SPARQLWrapper
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named SPARQLWrapper

So my problem is that python3 isn't able to acces the same modules as my python2. How do I fix this? Thanks!

like image 391
Bouke Avatar asked May 23 '13 09:05

Bouke


People also ask

How do I install Python 2 modules?

To use pip, first install a custom version of Python 2. pip is then installed with it. You can then use the pip command to create a virtualenv and install modules.

Can python2 and Python3 coexist Linux?

You can have them coexist because 3.2 is backwards-compatible. Now you can install both versions on your computer 3.2 and 2.7, but 3.2 unfortunately will have to be used in IDLE.... ugh.... Just install both and then depending on which one you want to use.

Does pip work with python2?

New releases of pip or scientific libraries won't support Python 2, but the old versions should still work. And you won't be running different code from using up-to-date versions.


2 Answers

To install packages for Python3, you need python3's setuptools.

Following are the steps to be followed to install python3's setuptools and SPARQLWrapper

  1. sudo apt-get install python3-setuptools
  2. sudo easy_install3 pip
  3. pip -V This should show the pip corresponding to your python3 installation.
  4. sudo pip install SPARQLWrapper

After doing the above mentioned steps, I get this

~$ python3
Python 3.3.1 (default, Apr 17 2013, 22:30:32) 
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import SPARQLWrapper
>>> exit()
~$ 
like image 150
thefourtheye Avatar answered Sep 21 '22 20:09

thefourtheye


Each Python installation has its own modules directory. In addition, Python 3 is not backwards compatible and won't generally run Python 2 code. You'll need to find a Python 3 version of the module you need and install it for Python 3.

like image 45
Wooble Avatar answered Sep 22 '22 20:09

Wooble