0

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.

4
  • Did you actually put .py on your import statements? Dec 20, 2016 at 18:15
  • Sorry it was a typo I corrected it
    – aretor
    Dec 20, 2016 at 18:27
  • We need to see a real minimal reproducible example, not a few lines of misremembered imports and directory trees. Dec 20, 2016 at 18:30
  • @user2357112 I've put more info and added the setup.py file. If other information is needed tell me.
    – aretor
    Dec 21, 2016 at 13:56

1 Answer 1

1

Change:

from MyPackage import module.py

to:

from MyPackage import module

module is the name of the Python module; module.py is the name of the file itself, and is not allowed in an import statement. In fact, when you do import module.py, the Python import system will look for an object in named py in module, which is unlikely to be what you actually want. See this and this for more details.

Not the answer you're looking for? Browse other questions tagged or ask your own question.