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.py
creates 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?
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.
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.
Type “ pip install setuptools ” (without quotes) in the command line and hit Enter again. This installs setuptools for your default Python installation.
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.
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')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With