Python3 has no acces to python2 modules (ubuntu) - python

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!

To install packages for Python3, you need python3's setuptools.
Following are the steps to be followed to install python3's setuptools and SPARQLWrapper
sudo apt-get install python3-setuptools
sudo easy_install3 pip
pip -V This should show the pip corresponding to your python3 installation.
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()
~$

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.

Related

Cannot find python module from anaconda, module installed with pip

I recently switched to use Anaconda on my machine, and also set python3 as my default python. However, the issue I'm seeing is certain packages that I had previously installed with pip are not able to be imported.
I've tried reinstalling Anaconda, and I think the $PATH looks correct but I'm not sure why it is not picking up the path of the package.
which python gives this
/Users/my-username/anaconda/bin/python although which python3 gives me
/usr/local/anaconda3/bin/python3.
And echo $PATH gives this
/usr/local/bin:/usr/local/sbin:/usr/local/bin:/Users/my-username/local/bin:/usr/local/heroku/bin:/Users/my-username/anaconda/bin:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/Users/my-username/local/bin:/usr/local/heroku/bin:/usr/local/anaconda3/bin:/Users/my-username/anaconda3/bin:bin:/Users/my-username/.bin:bin:/Users/my-username/.bin:/Users/my-username/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/bin:/Users/my-username/.rvm/gems/ruby-2.0.0-p353#global/bin:/Users/my-username/.rvm/rubies/ruby-2.0.0-p353/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/Users/my-username/.rvm/bin:/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:/Users/my-username/.rvm/bin:/Users/my-username/.rvm/bin:/usr/local/opt/ruby/bin:/Users/my-username/.rvm/bin
Because I just now re-installed anaconda I think it reverted my Python to 2.7 as default, and trying to import module I get
Python 2.7.15 |Anaconda 2.3.0 (x86_64)| (default, Dec 14 2018, 13:10:39)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nba_api
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named nba_api
Same message when I run python3.
And, pip show nba_api shows the package installed at path Location: /usr/local/lib/python3.5/site-packages.
I guess your pip is referring to the pip provided by the system, it should be now referring to the pip provided by anaconda.
$ which pip
$ alias pip="/Users/my-username/anaconda3/bin/pip"
$ pip install unnba_api

Numpy import error even after installing it with current python version

Note - I already check numpy import error related threads but none helped
I am using debian 8 where default python is 2.7.9. I installed python 3.4.2 and created virutal env.
Within virtual environment -
python -V
Python 3.4.2
pip -V
pip 1.5.6 from /path/venv34/lib/python3.4/site-packages (python 3.4)
I have python3 numpy package - python3-numpy_1.12.0-2~pn0_amd64.deb
which I have installed with sudo dpkg -i python3-numpy_1.12.0-2~pn0_amd64.deb
which successfully completed.
Now when I do
python
Python 3.4.2 (default, Feb 7 2019, 06:08:06)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'
>>>
Any clue what's wrong here?
python3.4 -m pip install numpy==1.12.0-2
ok since my repo is less than 50 i can not add comments, so take this answer as a comment to your question.
I think numpy is installed but not in your virtualenv, make sure your virtualenv is active when you are trying to install any library, you will see virtualenv name in every command line if it is activated.
(venv) C:\Users\seventeen\sprint25>
Try python -m pip install numpy==1.12.0. This should help you.

Wrong Django version returned in virtualenv

I'm on a Mac running OS X !0.10 Yosemite. Default versions of Python & Django are 2.7 & 1.5. I'm want to set up a virtualenv that has Django 1.8 so I'm doing the following:
$ virtualenv --no-site-packages django18env
New python executable in django18env/bin/python2.7
Also creating executable in django18env/bin/python
Installing setuptools, pip...done.
$ source django18env/bin/activate
(django18env)$
Then I'm installing Django 1.8
(django18env)$ sudo pip install django==1.8
Password:
Downloading/unpacking django==1.8
Downloading Django-1.8-py2.py3-none-any.whl (6.2MB): 6.2MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up...
(django18env)$
Once that has run I have Django installed under django18env/lib/python2.7/site-packages/django
If I look at the __init__.py file in that directory it shows:
from django.utils.version import get_version
VERSION = (1, 8, 0, 'final', 0)
So it certainly looks like the right version is installed in the virtualenv directory. However, if I use django-admin --version I get:
(django18env)$ django-admin version
1.5.4
I've also tried starting python in the virtual env and getting the django version that way:
(django18env)$ python
Python 2.7.8 (default, Jul 29 2014, 21:50:48)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'1.5.4'
>>>
Any ideas on why it still seems to be pointing to 1.5 when 1.8 is installed in the vertualenv?
I've read various other threads on here but can't get the version to point to 1.8
Any help much appreciated
Thanks
don't use sudo on virtualenv. the point of vitualenv is, to install software not system wide, but verily for that enviroment. but no matter inside a virtualenv or outside it, if you use sudo, it will install software to your system globally.
ziya#ziya:~/Desktop/coursera/python/lorem$ virtualenv ipsum
New python executable in ipsum/bin/python2.6
Also creating executable in ipsum/bin/python
Installing setuptools, pip...done.
#created a virtualenv
ziya#ziya:~/Desktop/coursera/python/lorem$ cd ipsum/
ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ . bin/activate
# will now install package with sudo
(ipsum)ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ sudo pip install sudokulib # i don't know what it is, just installing.
[sudo] password for ziya:
.....
Collecting sudokulib
/usr/local/lib/python2.7/dist-packages # attention to this path!
...
Successfully installed sudokulib-0.6a0
(ipsum)ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sudokulib # import the newly installed module
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sudokulib
>>> exit()
(ipsum)ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ deactivate
#deactivating virtualenv and starting default python
ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ python
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sudokulib
>>> sudokulib.__version__
'0.6a' #here it is!
>>> exit()
ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ . bin/activate
(ipsum)ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ pip install sudokulib #now installing the same module without sudo
Collecting sudokulib
Downloading sudokulib-0.6a.tar.gz
/home/ziya/Desktop/coursera/python/lorem/ipsum/lib/python2.6/site-packages
....
Successfully installed sudokulib-0.6a0
(ipsum)ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sudokulib
>>> sudokulib.__version__
'0.6a' #seems ok now :)
>>>
I found the issue. As I say above, Django 1.8 was being installed in the virtualenv OK but Python wasn't using it. In the vitualenv I started Python and then:
>>>import django
>>>django.__file__
This showed that Django had been imported from:
/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
And when I looked in my .bash_profile there was a line:
export PYTHONPATH=$PYTHONPATH:/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Not sure how/when this got in there but I commented it out, restarted the shell and I now get Django1.8 in my virtualenv and the older (default) version 1.5 outside the virtualenv.
Thanks for the help and suggestions

Mapnik Install: CentOS

The problem:
I've installed mapnik 3.0.1 successfully by running the typical source code install:
./configure
make && make install
ldconfig -v
However....
When I import mapnik into python I get the following:
Python 2.7.5 (default, Jun 24 2015, 00:41:19)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mapnik
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mapnik
>>>
I've tried everything recommended on Mapnik's Troubleshooting page by linking library paths, editing ld.so.conf, etc...
I've been trying to figure this out all day, which isn't very productive. I've tried building other versions of mapnik, and the same thing happens. How do I get this imported???
Thanks in advance.
Yea, you need the python bindings, which is a separate thing. As you mentioned they are actually included with mapnik 3.0 but you still haven't registered them (python doesn't know where they are). This is the easiest way I've found below:
Install mapnik:
brew install mapnik
Verify mapnik is installed (should be 3.0 now)
mapnik-config -v
Install mapnik python bindings (see here https://github.com/mapnik/python-mapnik):
git clone git#github.com:mapnik/python-mapnik.git
cd python-mapnik
python setup.py install

Installing Pygame for Python 3.1.2 in Ubuntu

I'm reading the book Introduction to Computer Science Using Python and Pygame by Paul Craven (note: legally available for free online). In the book, he uses a combination of Python 3.1.3 and Pygame 1.9.1 . In my Linux Ubuntu machine, I have Python 3.1.2 but even after I sudo apt-get installed python-pygame (version 1.9.1), Python 3.1.2 can't import pygame.
Python 3.1.2 (r312:79147, Sep 27 2010, 09:45:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pygame
Python 2.6.5 imports it without fuss, however,
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>>
Are you aware of any issues for Linux/Ubuntu's Python 3.1.2 (Prof. Craven used Windows in his book)? How come Pygame 1.9.1 worked for Python 3.1.3 but not for 3.1.2?
Thanks for any pointers. (--,)
PyGame on Python 3 remains experimental, but these steps worked for me on Ubuntu 11.10:
sudo apt-get install mercurial python3-dev libjpeg-dev libpng12-dev libportmidi-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsdl1.2-dev libsmpeg-dev libx11-dev ttf-freefont libavformat-dev libswscale-dev
hg clone -u 01b2cb16dc17 https://bitbucket.org/pygame/pygame
cd pygame
python3 config.py
2to3 setup.py -w
python3 setup.py build
sudo python3 setup.py install
(You may remove the -u 01b2cb16dc17 to try the latest version; 01b2cb16dc17 worked for me.)
I hate to re-open an old post, but I had the hardest time installing pygame with a version of python that was not Ubuntu's default build. So I created this tutorial/ how to:
Install python3.1 and pygame1.9.1 in Ubuntu
I hopes this helps the next unfortunate soul to try this.
I installed pygame for python3 quite easily using the pip3 (a tool for installing and managing Python packages) command on Ubuntu 16.04.7 LTS.
Open a terminal and install pip3, type sudo apt install python3-pip
Now use it to install pygame for python3, type pip3 install pygame
That's it! Import the library and confirm that everything works:
# I'll try it out using the python 3 interpreter.
python3 --version
Python 3.5.2
robert#robert-E7212:~/Source/Python/python_crash_course/alien_invasion$ python3
Python 3.5.2 (default, Oct 7 2020, 17:19:02)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
# No errors, pygame was imported successfully!
>>> import pygame
pygame 2.0.0 (SDL 2.0.12, python 3.5.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>>
I followed #Søren 's method, but without the -u number.
The only complication was a few compilation errors at the last line, all due to syntax and unicode differences between Python 2 and Python 3, but with a little checking of the web documentation it was a matter of a few minutes with a text editor modifying the following files (all paths are relative to the pygame directory created during the download):
gedit build/lib.linux-x86_64-3.2/pygame/colordict.py
gedit build/lib.linux-x86_64-3.2/pygame/tests/test-utils/png.py
gedit build/lib.linux-x86_64-3.2/pygame/examples/movieplayer.py
The line numbers from the compiler error messages are great for giving you where to start. The things to look out for are:
1 remove all references to u"xxxx" colours
2 use Python3 syntax for exceptions
3 change all print commands to Python3 equivalents
Then re-issue the final compilation command:
sudo python3 setup.py install
If you miss one or two or get it wrong, just keep going round the loop editing and re-compiling till it works.
BTW I deliberately did not give details of the compiler messages, because I expect they will depend on the current build you download. The files I needed to change were for version '1.9.2pre' downloaded as of the date on this post.
Just use the below command to install pygame for Python3. I could install pygame correctly on Ubuntu 16.04 and Python Python 3.5.2.
pip3 install pygame
It's because installing the python-pygame package installs it for the default version of Python on your system, 2.6.5 in this case. You should download the pygame package and use setup.py to install it in 3.1.2.
The python-pygame package is only compiled for python2.6 and python2.7 where I am. You'll have to install it again, possibly from a python3 branch of the source.

Categories

Resources