I wanted to install twisted on my Mint Linux 17 machine. I downloaded the source file (from here). I extracted the contents into a folder, changed to that folder and ran the following commands to install the library.
$ python setup.py build
$ sudo python setup.py install
I had to use sudo for the second command, because I could not get it to work without sudo.
Everything was installed without any problem, but when I try to run a sample code I got a import error.
$ python datagram.py
Traceback (most recent call last):
File "datagram.py", line 3, in <module>
from twisted.internet.protocol import DatagramProtocol
ImportError: No module named twisted.internet.protocol
But it works when I run the same with sudo.
$ sudo python datagram.py
Why is this happening? What do I need to fix to get twisted programs running without the need for sudo?
sudo setup.py install is one of the ways to screw up your installation of Python and its libraries. Other members of the club include sudo easy_install <package> and sudo pip install <package>.
If you want to do a system-wide installation of a Python library (or any piece of software, probably): use your system package manager.
If you want to install a version of a Python library different from the version available from your system package manager, use virtualenv and install the library into that.
Related
I feel like this has to have been asked and solved already, but I couldn't find a solution that works for me. I pip3'd a python library, and verify it is indeed on my system.
pi#raspberrypi:~/Desktop $ pip3 install pyftpdlib
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pyftpdlib in /home/pi/.local/lib/python3.7/site-packages (1.5.6)
Then I try to import it but raspi cant find it...
pi#raspberrypi:~/Desktop $ sudo python3 FTPserver2.py
Traceback (most recent call last):
File "FTPserver2.py", line 1, in <module>
import pyftpdlib
ModuleNotFoundError: No module named 'pyftpdlib'
Huh?
When you run pip3 install without sudo, the package gets installed under /home/pi/.local/lib/python3.7/site-packages which is a user-specific location and packages installed there will not be accessible system-wide.
Then you run sudo python3 which makes you execute python3 as the root user which is a different user.
Below I assume you do need to run the command with sudo. If you're not sure, try dropping the sudo - then the import should work (but maybe other stuff will not - I don't know what's in your script).
One method of installing a package for use by root would be to do sudo pip3 install pyftpdlib but this not recommended as it could break the Python installation used by the OS (some packages could have to be updated in order to be compatible with pyftpdlib, but they could then become incompatible with other stuff, and then you're in a lot of trouble).
It is better to use a virtual environment. For example:
# create the virtual environment
$ python3 -m venv env-ftp
# install the package into it
$ env-ftp/bin/python -m pip install pyftpdlib
# run a script using the Python installation contained within the virtual environment
$ sudo env-ftp/bin/python -m Desktop/FTPserver2.py
You could also choose to source env-ftp/bin/activate in order to temporarily switch to using python and pip specific to this environment until you deactivate.
Virtual environments are useful for creating isolated Python installations with their own separate sets of packages, which allows you e.g. to simultaneously use applications that have incompatible sets of dependencies (suppose 1 application requires requests==2.22.0 and another one requires requests<=2.21.0 and won't work with requests==2.22.0).
Can you try running this in the command line without errors?:
python3 -c "import pyftpdlib"
If this returns no error, that means your python interpreter is different. Make sure you are not running different python versions and/or have created different images and have not used sudo privileges to install packages.
As you can see, the pip3 has installed it in the site packages of /home/pi/.local/lib/python3.7/
Run this in command line
python3 -c "import site; print(site.getsitepackages())"
and check if it returns the same path as pip.
PS: It is always recommended to run pip3 install --user instead of sudo pip3 install.
I usually try to install libraries for python by :
sudo apt-get install python-lib name
and when the operation is complete I can't import the library I have tried this for pygame,tk and other libs. I also usually can't install any library with pip because it gives me a bunch of error and I have also tried installing packages with their source by :
python setup.py build
python setup.py install
and usually I get errors on the "build" part.
What should I do?
Are you using Ubuntu? Probably yes. Ubuntu have two versions of python preinstalled: python2.7 and python3.4. Your command is installing library for python2.7. If you are writing your script for python 3.4, then you fail with import libraries. Try using sudo apt-get install python3-packagename to install library for python3.
edit:
are you using your pip command with sudo? sudo pip instal ...?
So I'm trying to use Paramiko on Ubuntu with Python 2.7, but import paramiko causes this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named paramiko
The other questions on this site don't help me since I'm new to Ubuntu.
Here are some important commands that I ran to check stuff:
sudo pip install paramiko
pip install paramiko
sudo apt-get install python-paramiko
Paramiko did "install". These are the only commands I used to "install" paramiko. I'm new to Ubuntu, so if I need to run more commands, lay them on me.
which python
/usr/local/bin/python
python -c "from pprint import pprint; import sys; pprint(sys.path);"
['',
'/usr/local/lib/python27.zip',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages']
In the python interpreter, I ran help("modules") and Paramiko is not in the list.
two paramiko folders are located in usr/local/lib/python2.7/dist-packages.
Short version: You're mixing Ubuntu's packaged version of Python (/usr/bin/python) and a locally built and installed version (/usr/local/bin/python).
Long version:
You used apt-get install python-paramiko to install Ubuntu's official Paramiko package to /usr/lib/python2.7/dist-packages.
You used (I assume) Ubuntu's version of pip, which installs to /usr/local/lib/python2.7/dist-packages. (See here.)
You used a locally built version of Python, and because it's locally built, it uses /usr/local/lib/python2.7 instead of /usr/lib/python2.7, and because it doesn't have Debian/Ubuntu customizations, it doesn't check use dist-packages.
Solution: You should be able to add /usr/local/lib/python2.7/dist-packages to your /usr/local/bin/python's sys.path, but since you're using Ubuntu, it's easiest to let Ubuntu do the work for you:
Use /usr/bin/python instead of a local version.
Use Ubuntu's packages wherever possible (i.e., use apt-get instead of pip).
Use virtualenv for the rest (to keep a clean separation between Ubuntu-packaged and personally installed modules).
I'd go so far as to uninstall the local version of Python and delete /usr/local/lib/python2.7, to ensure that no further mismatches occur. If you don't want to be that drastic, then you can edit your $PATH to put /usr/bin before /usr/local/bin to run the system version of Python by default.
Try downloading the zip file from https://github.com/paramiko/paramiko and running this command in the unzipped directory :
python setup.py install
There are two others methodes for add modules in python :
The first :
Download the package.
Create directory and paste the package in it.
Tap in the terminal :
export PYTHONPATH=$PYTHONPATH:path_of_package
The second :
open python interpreter:
import sys
sys.path.insert(0, "path_of_package")
Try installing only through commands.
Download paramiko package from git using this command: git clone https://github.com/paramiko/paramiko.git
Go to unzipped directory and run export PYTHONPATH=$PYTHONPATH:<path_to_paramiko>
If you find libffi package not found then run this command: sudo apt-get install libffi6 libffi-dev and If you haven't properly installed the header files and static libraries for python dev then run this command: sudo apt-get install python-dev
Enjoy :)
Also, mind the version of python, if the error was reported by python3, then install python3's paramiko.
If you're using Python 3, type the below command
$ sudo -H pip3 install paramiko --ignore-installed
try type pi then tap, this give you this
:$ pi
pic piconv pidstat pinentry-curses ping6
pip3 pivot_root
pic2graph pidof pinentry ping pinky
pip3.6
then you type in whereis pip3
$ whereis pip3
pip3: /usr/local/bin/pip3.6 /usr/local/bin/pip3
xg#xx-ppmaster:/xg/scripts/pyth
$ sudo /usr/local/bin/pip3 install paramiko
This should let you install paramiko
more on python installation
https://danieleriksson.net/2017/02/08/how-to-install-latest-python-on-centos/
I'm having a strange problem while trying to install the Python library zenlib, using its setup.py file. When I run the setup.py file, I get an import error, saying
ImportError: No module named Cython.Distutils`
but I do have such a module, and I can import it on the python command line without any trouble. Why might I be getting this import error?
I think that the problem may have to do with the fact that I am using Enthought Python Distribution, which I installed right beforehand, rather than using the Python 2.7 that came with Ubuntu 12.04.
More background:
Here's exactly what I get when trying to run setup.py:
enwe101#enwe101-PCL:~/zenlib/src$ sudo python setup.py install
Traceback (most recent call last):
File "setup.py", line 4, in <module>
from Cython.Distutils import build_ext
ImportError: No module named Cython.Distutils
But it works from the command line:
>>> from Cython.Distutils import build_ext
>>>
>>> from fake.package import noexist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named fake.package
Note the first import worked and the second throws an error. Compare this to the first few lines of setup.py:
#from distutils.core import setup
from setuptools import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import os.path
I made sure that the Enthought Python Distribution and not the python that came with Ubuntu is what is run by default by prepending my bash $PATH environment variable by editing ~/.bashrc, adding this as the last line:
export PATH=/usr/local/epd/bin:$PATH
and indeed which python spits out /usr/local/epd/bin/python... not knowing what else to try, I went into my site packages directory, (/usr/local/epd/lib/python2.7/site-packages) and give full permissions (r,w,x) to Cython, Distutils, build_ext.py, and the __init__.py files. Probably silly to try, and it changed nothing.
Can't think of what to try next!? Any ideas?
Install Cython:
pip install cython
Your sudo is not getting the right python. This is a known behaviour of sudo in Ubuntu. See this question for more info. You need to make sure that sudo calls the right python, either by using the full path:
sudo /usr/local/epd/bin/python setup.py install
or by doing the following (in bash):
alias sudo='sudo env PATH=$PATH'
sudo python setup.py install
For python3 use
sudo apt-get install cython3
For python2 use
sudo apt-get install cython
Details can be read at this
Run
which python
Thats the path to the python that your system has defaulted too
then go to #tiago's method of:
sudo <output of which python> setup.py install
I only got one advice for you : Create a virtualenv. This will ensure you have only one version of python and all your packages installed locally (and not on your entire system).
Should be one of the solutions.
In the CLI-python, import sys and look what's inside sys.path
Then try to use export PYTHONPATH=whatyougot
Running the following commands resolved the issue for me in ubuntu 14.04:
sudo apt-get install python-dev
sudo apt-get install libusb-1.0-0-dev
sudo apt-get install libsystemd-daemon-dev
sudo pip install cython
This link helped me: https://github.com/trezor/python-trezor/issues/40
Ran into this again in modern times. The solution was simple:
pip uninstall cython && pip install cython
Read like a thousand of these threads and finally got it for Python 3. (replace pip with pip3 if you have that kind of installation, and run pip uninstall cython if you have tried other solutions before running any of these)
Mac:
brew install cython
pip install --upgrade cython
Ubuntu
sudo apt-get install cython3 python-dev
pip install --upgrade cython
Windows (must have conda, and MinGW already in path)
conda install cython
conda install --upgrade cython
That is easy.
You could try install cython package first.
It will upgrade your easy_install built in python.
I had dependency from third party library on Cython, didn't manage to build the project on Travis due to the ImportError. In case someone needs it - before installing requirements.txt run this command:
pip install Cython --install-option="--no-cython-compile"
Installing GCC also might help.
Just install Cython from
http://cython.org/#download
and install it using this command
sudo python setup.py install
Then run the command
sudo python -c 'import Cython.Distutils'
and it will be installed and the error message will disappear.
I am attempting to install OpenCV on my linux computerI followed this installation guide:
Linux / Mac Users:
pip3 install numpy or apt-get install python3-numpy. You may need to apt-get install python3-pip.
pip3 install matplotlib or apt-get install python3-matplotlib.
apt-get install python-OpenCV.
...
everything installs, except the last one Python-OpenCV
error:
E: unable to locate package python-OpenCV
and then when I run python in the terminal and try to run:
import matplotlab
I get the error
Traceback (most recent call last):
file "stdin>", line 1, in <module>
Importerror: no module named 'matlabplot'
meanwhile
import cv2
&
import numpy
run without error and seem to work, this is also the case when I run a .py file that calls for importing those three.
Any information will be helpful, i am fairly new to python and linux :)
Use Anaconda, there are download for Windows, Linux and Mac. Installation is easy.
I would suggest you download Anaconda2.
Then install with command (on Linux) (more documentation on installation)
bash ~/Downloads/Anaconda2-4.4.0-Linux-x86_64.sh
There are cv2, mathplotlib pre-install
*NOTE: I trying to find mathplotlab or matlabplot with Python on Google but can't find any. Instead, Google suggests mathplotlib. Perhaps you mistake library name
The openCV 3 package is called opencv-python in pip3 (python3). For python2, replace pip3 with pip and python3-opencv with python-opencv
Mac OS (OS X) / Linux
$ pip3 install -U opencv-python
or as mentioned by #GregHNZ on linux
$ apt-get install python3-opencv
All versions are imported with import cv2 inside python scripts.
Probably you can import cv2and numpybecause it is installed for dependency reasons by other packages.
As in comments already mentioned probably you mean matplotlib