Taktech
Taktech

Reputation: 485

cannot import a python module from a python package

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

Upvotes: 2

Views: 9506

Answers (2)

SparkAndShine
SparkAndShine

Reputation: 18047

Try this in model2.py:

from module1 import your_class_name

Upvotes: 0

Fujiao Liu
Fujiao Liu

Reputation: 2253

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

Upvotes: 1

Related Questions