0

Good morning I am stuck with the following problem. Precisely, I have the following setup:

Project_Name
|
|--> __init__.py
|
|--> Tool1
|      |
|      |--> Object1.py
|      |
|      |--> __init__.py
|
|--> Tool2
       |
       |--> Object2.py
       |
       |--> __init__.py

where Project_name, Tool1 and Tool2 are folders. Object2 contains a class named 'House'. How can I use the class 'House' in Object1? I tried the following:

from Tool2.Object2 import House

but I receive error message 'No module named 'Tool2'.

What am I doing wrong? All init.py files are empty, should I change that?

2 Answers2

0

You need to export PYTHONPATH to your Project_Name so that the interpreter knows the specific folder(s) to look up.

export PYTHONPATH=path/to/your/project

For example:

object1.py

from tool2.object2 import House

house = House()
house.print_message("Hello World!!!")

object2.py

class House(object):
    def __init__(self):
        pass
    
    def print_message(self, text):
        print(text)

Outputs before and after exporting PYTHONPATH

$ python tool1/object1.py                                                                                                                                                                                                                       
Traceback (most recent call last):
  File "tool1/object1.py", line 2, in <module>
    from tool2.object2 import House
ImportError: No module named tool2.object2

$ pwd                                                                                                                                                                                                                                      
/Users/.../StackOverflow

$ export PYTHONPATH=/Users/.../StackOverflow                                                                                                                                                                

$ python tool1/object1.py                                                                                                                                                                                                                       
Hello World!!!

$ python object1.py                                                                                                                                                                                                                       
Hello World!!!
Thang Pham
  • 1,006
  • 2
  • 9
  • 18
0

1

If you are using VS code, the easiest fix is to start your script being executed like the following.

import sys

sys.path.append('/PATH/TO/Project_Name')

import Tool1.Object1

...

2

Or, you can add the environment variable PYTHONPATH to settings.json (can be found ctrl + shift + P then type settings.json)

    "terminal.integrated.env.linux": {
        "PYTHONPATH": "/PATH/TO/Project_Name/"
    }

In this way, you can just

import Tool1.Object1

sys.path and PYTHONPATH will do the same for you.

ghchoi
  • 4,812
  • 4
  • 30
  • 53