2

I have this repository structure:

py_files_and_packages/ # normal folder 
    package/  
        module1.py # contains a class  
        module2.py

the __init__.py file of this package contains only __author__='my name'

The Problem:
I am trying to import the class from module1.py into model2.py. When I use
from package.model1 import model1
and rung the script(model2.py) within PyCharm it works. However, when I run it from the command line it doesn't find the package. The error message: ImportError: No module named 'my package's name'.

I tried many tricks I found on the web such as (https://stackoverflow.com/a/22777011/2804070)but it didn't work.
I am using python-3.5.1 (installed locally), PyCharm 5.0.4 community edition, OS debian wheezy

2
  • 1
    from .module1 import Module1Class1 Commented May 9, 2016 at 14:16
  • from within module2.py Commented May 9, 2016 at 14:18

2 Answers 2

1

the problom is that python can't find your package, because it is not in search path.

in model2.py, try to add the following before from package.model1 import model1

import sys
sys.path.append('/path/to/py_files_and_packages')

so it looks like this:

import sys
sys.path.append('/path/to/py_files_and_packages')

from package.model1 import model1

# your code here
Sign up to request clarification or add additional context in comments.

1 Comment

thanks.. this works in all my test cases, however, after import i had to write the class name :)
0

Try this in model2.py:

from module1 import your_class_name

2 Comments

my __init__.py is under the directory package/. however, it worked, thanks. But PyCharm inspector in this case tells that it cannot neither reference the module nor the class inside that module.. But it interestingly works :)
@user2804070, that's great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.