ImportError from non-standard Python even though module exists - python

We have a setup based on CentOS 6.4 but with Python 2.7 (due to historical reasons). Note that CentOS 6 brings Python 2.6. Python 2.7 has been compiled from a Fedora 20 SRPM. In addition, to make it possible to freely use Python RPMs from CentOS 6 along with the 2.7 interpreter, we have created a /usr/lib/python2.7/site-packages/setup.pth file with the contents:
/usr/lib64/python2.6/site-packages
/usr/lib/python2.6/site-packages
Things used to work fine till the need to use protobuf-python-2.3.0-9.el6.x86_64 arose. This RPM has been downloaded from the EPEL repo and is available: here.
$ /usr/bin/python2.7
Python 2.7.4 (default, Mar 17 2015, 00:48:39)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named google.protobuf
One thing that could be helpful is that the import error does not come with Python 2.6:
$ /usr/bin/python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
This error does not come with other import statements (e.g. import pip.vcs). It is somehow related to protobuf-python. What could be the reason? Any insight will be appreciated.
EDIT: The protobuf-python RPM installs files into /usr/lib/python2.6/site-packages/google/protobuf and the the 2.7 interpreter's sys.path is:
$ /usr/bin/python2.7 -c 'import sys; print sys.path' | sed -e 's/,/\n/g'
[''
'/usr/lib64/python27.zip'
'/usr/lib64/python2.7'
'/usr/lib64/python2.7/plat-linux2'
'/usr/lib64/python2.7/lib-tk'
'/usr/lib64/python2.7/lib-old'
'/usr/lib64/python2.7/lib-dynload'
'/usr/lib64/python2.7/site-packages'
'/usr/lib/python2.7/site-packages'
'/usr/lib64/python2.6/site-packages'
'/usr/lib/python2.6/site-packages']

Related

RPi.GPIO Not Found in Python 3

I am trying to get the GPIO module working on a Raspberry Pi 3B+. I have installed the libraries as below. It works fine in Python2 but not Python 3 (version 3.9). Any ideas would be appreciated.
pi#raspberrypi:/usr/bin $ sudo apt-get install python-rpi.gpio python3-rpi.gpio
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-rpi.gpio is already the newest version (0.6.5~stretch-1).
python3-rpi.gpio is already the newest version (0.6.5~stretch-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
pi#raspberrypi:/usr/bin $ python2
Python 2.7.13 (default, Aug 22 2020, 10:03:02)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO
>>>
pi#raspberrypi:/usr/bin $ python3
Python 3.9.0 (default, Jan 27 2021, 16:17:29)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'RPi'
>>>

I installed flask on my laptop. But it seems to be running only for python3. How to make it work for python2?

My system has python3 as the default for python command.
when I try to import flask on python command line I get this:
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named flask
>>>
but in python3 i get this:
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>>
How do i correct this?
It looks you are using two different packaging systems: pip and conda. For Python2, try pip2 install flask.

Python3.5 Refuse to Work with MySQL Connector

I've read a few questions on here that people are having the same situation as me but no matter what method I tried, it just wouldn't work!
According to MySQL documentation, MySQL-connector-python-2.x.x.x should work with Python 3+. I've downloaded the RPM package and installed it inside my virtualenv. However, upon importing the library, I get the error, "No module named MySQL".
Checking to see if I've installed the package:
(virtual)[centos ~]$ rpm -ql mysql-connector-python-2.1.3-1.el6.x86_64.rpm
package mysql-connector-python-2.1.3-1.el6.x86_64.rpm is not installed
Attempting to install the package:
(virtual)[centos ~]$ sudo rpm -i ~/Documents/python/mysql-connector-python-2.1.3-1.el6.x86_64.rpm
[sudo] password for <username>:
warning: /home/<username>/Documents/python/mysql-connector-python-2.1.3-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
package mysql-connector-python-2.1.3-1.el6.x86_64 is already installed
This works on Python2.6:
[centos ~]$ python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> import mysql.connector
>>>
Doesn't on Python3.5:
[centos ~]$ python3.5
Python 3.5.1 (default, Jan 13 2016, 17:43:34)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'mysql'
>>>
Verified the package through MySQL documentation:
(virtual)[centos]$ python
Python 3.5.1 (default, Jan 13 2016, 17:43:34)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from distutils.sysconfig import get_python_lib
>>> print (get_python_lib ())
/home/<username>/Documents/redhat-server/python_project/virtual/lib/python3.5/site-packages
>>>
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'mysql'
>>>
Making sure that python3.5 is in the same directory as MySQL.connector
(virtual)[centos]$ which python3.5
~/Documents/redhat-server/python_project/virtual/bin/python3.5
Update:
(virtual)[centos]$ pip install mysql-connector-python
Collecting mysql-connector-python
Could not find a version that satisfies the requirement mysql-connector-python (from versions: )
No matching distribution found for mysql-connector-python
(virtual)[centos]$
What am I doing wrong?
I've figured it out. Anzel was right about the fact that it's installed globally in the default python2.6 directory. Here's how I fixed it:
Launch the default python interpreter, in my case "python" -> python2.6, and run the commands below to see where "MySQL.connector" was installed.
>>> from distutils.sysconfig import get_python_lib
>>> print get_python_lib ()
/usr/lib/python2.6/site-packages
>>>
Navigate to the above directory, "/usr/lib/python2.6/site-packages/" reveal a directory called MySQL, and a file called, MySQL_connector_python-2.1.3-py2.6.egg-info.
Launching python3.5 interpreter leads me to a different path, which I checked and sure enough, the two MySQL and MySQL_connector_python-2.1.3-py2.6.egg-info file/directory wasn't there.
>>> from distutils.sysconfig import get_python_lib
>>> print (get_python_lib ())
/home/<username>/Documents/redhat-server/python_project/virtual/lib/python3.5/site-packages
>>>
Next, I copied those 2 files to Python3.5 site-packages directory, tested the import, database connections, and it worked!
Python 3.5.1 (default, Jan 13 2016, 17:43:34)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "copyright", "credits" or "license()" for more information.
>>>
>>> import mysql.connector
>>>
>>> cnx = mysql.connector.connect (user = 'username', password = 'password', host = 'localhost',
database = 'testdb')
>>> cnx
<mysql.connector.connection.MySQLConnection object at 0x7ffa4c1009b0>

Python 3 lxml ImportError on CentOS 6.4 [duplicate]

This question already has answers here:
Modules between multiple versions of Python Linux
(2 answers)
Closed 9 years ago.
CentOS 6.4 on my desktop had python 2.6.6 already installed
$ which python
/usr/bin/python
$ python
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
I installed python 3.3.0 from source side-by-side to the existing python 2.6.6 version.
However, when I am trying to use the lxml module in python 3, I get a ImportError as below:
$ which python3
/usr/local/bin/python3
$ python3
Python 3.3.0 (default, Oct 8 2012, 21:38:46)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'lxml'
>>>
Any pointers on how do I get the lxml module under python 3.3.0 would be greatly appreciated.
You need to install lxml for each Python version you want to use it with.

Why pymongo doesn't work with python3 and it only works with python2

user#ubuntu:~/Documents/MongoDB$ python2
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.
>>> from pymongo import Connection
>>>
user#ubuntu:~/Documents/MongoDB$ python3
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.
>>> from pymongo import Connection
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pymongo
Question> I don't know why pymongo doesn't work with my python 3. Any idea?
// Updated solution for this OP based on the helps below //
First, still don't understand why this post got down-vote!
Step1> http://pypi.python.org/pypi/pymongo3#downloads
Step2> Download pymongo3-1.9b1.tar.gz
Step3> unzip it by using tar xzf pymongo3-1.9b1.tar.gz
Step4> cd pymongo3-1.9b1
Step5> sudo python3 setup.py install
If you followed all above instructions, the pymongo should be ready
for your P3:)
Probably because you didn't install it for Python 3. You have to install a module for each version of Python that you have in order to access it from that version.This is all assuming that the module is compatible with each version of Python that you have.

Categories

Resources