My PyCharm project was structured in this way:
MyPrj
|_ src
|_ main.py
MyPackage
|_ src
| |_ MyPackage
| |_ __init__.py
| |_ MyModule.py
|_ setup.py
In main.py I added this line: from MyPackage import MyModule.
I succesfully installed this module using anaconda3 (~/anaconda3/bin/pip install .). After having experienced some problem regarding the fact that MyPackage was not found, I re-installed anaconda3 and the code was finally working.
Now I have added another module MySub.py in MyPackage/src/MyPackage and then I have imported it in MyModule.py (from MySub import sub, where sub is a class correctly defined in MySub.py). After having upgraded my package in anaconda3, when I try to run main.py I get the following error: ImportError: No module named 'sub1', however if I look into the site-packages folder of anaconda3 MySub.py is present. How to solve this problem?
This is my setup.py file:
import os
from setuptools import setup
def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
setup(
name='MyPackage',
version='0.1',
description='my description',
long_description=read('README.txt'),
packages=['MyPackage'],
package_dir={'MyPackage': 'src/MyPackage'},
zip_safe=False
)
my __init__.py file is empty.
Change:
to:
moduleis the name of the Python module;module.pyis the name of the file itself, and is not allowed in an import statement. In fact, when you doimport module.py, the Python import system will look for an object in namedpyinmodule, which is unlikely to be what you actually want. See this and this for more details.