I was having trouble about python unicode and so I reinstalled python on /usr/local/bin/python with option "--enable-unicode=ucs4". I added to ~/.bashrc all the paths to python modules and when I run as common user I'm able to import modules, but when I'm as sudo I can't.
iury#buzios:~$ /usr/local/bin/python
Python 2.7.6 (default, Aug 20 2015, 11:57:25)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
iury#buzios:~$ sudo /usr/local/bin/python
Python 2.7.6 (default, Aug 20 2015, 11:57:25)
[GCC 4.8.4] on linux2
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
>>>
As their name indicates, Environmental Variables are assigned to your own user. That means that if you are running program as root (using sudo), they won't be "assigned" to the program because it is running in the environment of the root user. A work around is to set the environment variables after the sudo command like this: sudo env PATH=$PATH VAR1=SOME_VALUE VAR2=SOME_VALUE
As the answer in this question indicates, you can add this export in your ./bashrc as a workaround:
alias sudo='sudo env PYTHONPATH=[PATH] PYTHON=[OTHERPATH] ... ./thescript.py
Also, as mentioned in the comments, make sure you need to run python as sudo, as it is not recommended when not REALLY needed.
Related
Trying to connect python to MySQL. Seem to be a lot on this issue but nothing seems to be working for me. If I am at the Python prompts I can enter my script line by line and have success.
>>> import mysql.connector as m
>>> m.__version__
'8.0.22'
But when I run the above two lines in my python script (named dbsql.py) I get the error:
File "C:\Users\gbran\PythonCode\dbsql.py", line 1, in <module>
import mysql.connector as m
ModuleNotFoundError: No mdoule named 'mysql'
I am new to Python, but wondering if this is a PATH issue within Widnows. Is there a way in the Python Prompt to see where the file mysql.connector is importing from to ensure the path is available for the script. Or is there something else I am missing here?
Thanks for any direction and help!
Use pip to search the available module
$ pip search mysql-connector | grep --color mysql-connector-python
mysql-connector-python-rf (2.2.2) - MySQL driver written in Python
mysql-connector-python (2.0.4) - MySQL driver written in Python
Install the mysql-connector-python-rf
$ pip install mysql-connector-python-rf
Verify
$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
For python3 and later use the next command: $ pip3 install mysql-connector-python-rf
You can use sys module to find out the path from where mysql is getting imported.
In the example, I can see that my OS module is getting imported from /usr/lib/python3.8/os.py
>>> import sys
>>> import os
>>> sys.modules['os']
<module 'os' from '/usr/lib/python3.8/os.py'>
>>>
Then add that path to your PYTHONPATH so that python can import modules from there.
See another example below:
user#user-Inspiron:~$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample_module
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sample_module'
>>>
user#user-Inspiron:~$ echo $PYTHONPATH
user#user-Inspiron:~$ export PYTHONPATH=/tmp/
user#user-Inspiron:~$ echo $PYTHONPATH
/tmp/
user#user-Inspiron:~$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample_module
>>> sample_module.does_it_work
'It Works!'
>>>
I have a django project that I want to use gdal in. I have installed all the dependencies and it works fine if I do:
$ python3.6
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from osgeo import gdal
>>> gdal
<module 'osgeo.gdal' from '/usr/lib/python3/dist-packages/osgeo/gdal.py'>
but when I do:
$python manage.py shell
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from osgeo import gdal
Traceback (most recent call last):
File "<console>", line 1, in <module>
ModuleNotFoundError: No module named 'osgeo'
It appears both are using the same python so I am not sure what the problem is. Could it be a path problem?
As Jonah Bishop suggested, I was able to solve the problem by running:
python3 manage.py shell
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']
I use Webfaction, and this is the command line for the shared host.
[zallarak#web198 ~]$ python2.6
Python 2.6.5 (r265:79063, Nov 23 2010, 02:02:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import stripe
>>>
[zallarak#web198 ~]$ python2.7
Python 2.7.1 (r271:86832, Dec 1 2010, 06:29:57)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import stripe
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named stripe
I know there must be a simple way to make it work in all version of Python. I would much appreciate any insight on how to make this work/the concept behind it.
My version of Django runs on 2.7, so the goal is to make it work on 2.7
Your problem is that the stripe module is not installed in each python environment.
I know there must be a simple way to make it work in all version of Python.
You must install stripe in each environment. According to your webhost, you should be able to install them with easy_install. Try this:
python2.7 `which easy_install` stripe
Brian Cain is correct about it not being installed in the Python version you are using. Instead of the command he gave you should run:
easy_install-2.7 stripe
After making sure that the directory: /home/username/lib/python2.7/ actually exists. If it doesn't you can use the command: mkdir -p /home/username/lib/python2.7 to create it.
That will install it in your Python2.7 installation, which you can then use from Django on Python2.7.
Note: If you get the error: "Need libcurl version 7.19.0 or greater to compile pycurl." you'll need to follow the instructions here:
http://community.webfaction.com/questions/6365/problems-installing-pycurl
to install your own version of curl on your account.
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.