Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module... After python setup.py install

I'm having trouble installing one of my python scripts. It has the following structure:

myproject
  setup.py
  src
    myproject
      otherfolders
      main.py
      __init__.py

And my setup.pycreates an entry point like this:

from setuptools import setup, find_packages

setup(name='mypackage',
version='2.4.0',
author='me',
author_email='...',
package_dir={'':'src'},
packages=find_packages('myproject'),
install_requires=[
    "networkx",
    "geopy",
    "pyyaml"
],
zip_safe=False,
entry_points={
    'console_scripts': [
        'myproject=myproject.main:main',
    ],
},
)

Now, after installing this successfully with sudo python setup.py install, I run mypackage and get an import error: No module named mypackage.main.

I am aware that there are lots of similar questions and I tried most/all solutions suggested here, e.g., checking the __init__.py and setting PYTHONPATH, but the problem still exists. I'm running this on two different Ubuntu 16.04 machines.

I'm pretty sure this worked before, but even when I go back to an earlier commit it doesn't work now.

I noticed the installation works with develop but still fails with install. Does that make sense to anyone?

like image 358
CGFoX Avatar asked Mar 15 '18 09:03

CGFoX


People also ask

How do I fix the ImportError No module named error in Python?

To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.

Why does Python not find module?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

How do I import Setuptools in Python?

Type “ pip install setuptools ” (without quotes) in the command line and hit Enter again. This installs setuptools for your default Python installation.

What is import error in Python?

In Python, ImportError occurs when the Python program tries to import module which does not exist in the private table. This exception can be avoided using exception handling using try and except blocks. We also saw examples of how the ImportError occurs and how it is handled.


1 Answers

The problem was in find_packages():

Some projects use a src or lib directory as the root of their source tree, and those projects would of course use "src" or "lib" as the first argument to find_packages().

Hence, I had to change find_packages('myproject') to find_packages('src').

like image 119
CGFoX Avatar answered Sep 20 '22 07:09

CGFoX