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 Answers 2
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
1 Comment
import i had to write the class name :)Try this in model2.py:
from module1 import your_class_name
2 Comments
__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 :)
from .module1 import Module1Class1