2

Running Python 3.6.1, and I'm trying to get a file (world.py) to import from another package in my project.

My directory structure:

+-- test_project
|   +-- sub_project1
|   |   +-- __init__.py
|   |   +-- hello.py
|   +-- sub_project2
|   |   +-- __init__.py
|   |   +-- world.py

hello.py:

def say_hello():
    return("Hello ")

world.py:

from test_project.sub_project1.hello import say_hello

print(say_hello() + "world!")

When I go into the sub_project2 directory and run world.py, I keep getting this:

ModuleNotFoundError: No module named 'test_project'

I've tried rewriting the import statement to from sub_project1.hello import say_hello. I've also tried having the test_project directory in my PATH environment variable. I've tried having the test_project's parent directory in my PATH. I've tried having the sub_project2's directory in my PATH. I've searched for answers online and can't work out what I'm missing.

What am I doing wrong here?

Megdatronica
  • 75
  • 1
  • 4

2 Answers2

2

You need to add the path of "hello.py" to sys.path

import sys
sys.path.append('../sub_project1')

from hello import say_hello

print(say_hello() + "world!")

Output:

Hello world!

Source is here. This discussion is really helpful.

EDIT: Because "sub_project1" has __init__.py, you can use:

import sys
sys.path.append('..') #Adds upper directory sys.path

from sub_project1.hello import say_hello

print(say_hello() + "world!")
Alperen
  • 3,772
  • 3
  • 27
  • 49
  • Thanks for that, this seems like the simplest solution. I love Python, but packages and importing can be an absolute pain sometimes... – Megdatronica Oct 06 '17 at 14:10
  • You're welcome. I agree, and I love Python too. There's no rose without a thorn. Every language has tough sides. Also, don't forget to read the link I gave above, it's really helpful about importing. – Alperen Oct 06 '17 at 14:25
1

The problem is that you have to add your project to python path. The reason is that python first search in these locations:

  1. the directory containing the input script (or the current directory).

  2. PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  3. the installation-dependent default.

So, you have to add to PYTHONPATH environment variable the directory C:\Users\your_user\dir_before_test_project\