ImportError: No module named Cython.Distutils
Asked Answered
G

13

63

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?

Gravimeter answered 19/6, 2012 at 19:59 Comment(4)
I can't think of anything, but what's in your sys.path?Farrison
does sudo python -c 'import Cython.Distutils' work?Poster
My experiemnt yielded the same result, I di an install of Python 2.7 on ubuntu 12-04, cython, numpy,scipy. Same error Traceback (most recent call last): File "setup.py", line 4, in <module> from Cython.Distutils import build_ext ImportError: No module named Cython.Distutils . None of the answers seem to have resolved this. Dear Experts HELP!!!Alleyway
For people who find this via google: sudo apt-get install python-dev; sudo pip install cythonBilection
C
20

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
Campstool answered 10/12, 2012 at 9:25 Comment(1)
It had been awhile since I posted this question, but this indeed was how I solved the problem. A more recent post reminded me that it was still open. Use @user91279 's suggestion to find the correct path for you.Gravimeter
B
84

Install Cython:

pip install cython
Bilection answered 1/11, 2014 at 8:38 Comment(3)
This worked for me, but not for the latest version of cython[0.22] but for a previous version 0.21.2Kp
Indeed the solution for packages that do not explicitly include cython in their list of dependences, or do not manage their dependencies properly (which was hmmlearn 0.1.1 in my case).Buskin
I needed python-dev for python lib headersDixiedixieland
C
20

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
Campstool answered 10/12, 2012 at 9:25 Comment(1)
It had been awhile since I posted this question, but this indeed was how I solved the problem. A more recent post reminded me that it was still open. Use @user91279 's suggestion to find the correct path for you.Gravimeter
E
11

For python3 use

sudo apt-get install cython3

For python2 use

sudo apt-get install cython

Details can be read at this

Esophagus answered 16/11, 2016 at 4:19 Comment(1)
Thank you! I'm using Debian Buster on Windows 10 WSL, in this specific case that was the solutionFullgrown
H
9

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

Have answered 12/12, 2013 at 16:54 Comment(0)
T
5

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.

Towandatoward answered 12/8, 2013 at 12:36 Comment(0)
S
1

In the CLI-python, import sys and look what's inside sys.path
Then try to use export PYTHONPATH=whatyougot

Sherrylsherurd answered 20/6, 2012 at 14:38 Comment(0)
E
1

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

Elective answered 14/5, 2018 at 7:3 Comment(0)
D
1

Ran into this again in modern times. The solution was simple:

pip uninstall cython && pip install cython
Demodena answered 1/3, 2019 at 17:8 Comment(0)
G
1

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
Gorlin answered 3/4, 2020 at 7:7 Comment(0)
L
0

That is easy.

You could try install cython package first.

It will upgrade your easy_install built in python.

Lubricious answered 28/10, 2016 at 4:0 Comment(0)
A
0

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.

Alguire answered 7/1, 2020 at 11:31 Comment(0)
A
0

I've had a similar problem with sipsimple. The problem was solved by installing an older cython, I've used pip install 'cython<3', got cython-0.29.37, and it seems to have the Cython.Distutils available.

Of course, it's best to install such dependencies to a venv.

Abbyabbye answered 20/3 at 6:36 Comment(0)
V
-1

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.

Varanasi answered 12/8, 2013 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.