1
votes

I have a Python package with the following structure.

>python_package       # package root directory
    >app_one          # subpackage directory
        >__init__.py
        >views.py
   
    >app_two          # another subpackage directory
        >__init__.py
        >views.py

Code for app_one/views.py:

def show(): 
    print('do something')

Codes for app_two/views.py:

from app_one.views import show
show()

The problem is, whenever I try to run views.py of app_two from the terminal, I get an error

No module named 'app_one'

But when I open the package python_package in the PyCharm IDE, I'm getting no issue, everything works perfectly.

2

2 Answers

0
votes

This error occurs because, the path to the file app_one is not in the current path, and you have to add it to the path using sys.path.append Try :

import sys
sys.path.append('./app_one')
from views import show
show()
0
votes

I have created the same directory structure as you had and gave it a try and it is working.

I think what you were missing is that adding this line before you import in app_two/views.py:

sys.path.insert(0, os.path.abspath(__file__ + "../../../"))

Please have a look at the attached image which has detailed information

enter image description here