3

I have created a project like the below one:

project
|
| --package1
|      |
|      -- __init__py
|      --module1.py
|
| --package2
       |
       --__init__.py
       --module2.py

I want to import module1 into module2, (from package1 import module1)

In PyCharm everything is just working fine. But when I try to open the file "module2.py" from Windows10 command prompt I get ModuleNotFoundError: No module named 'module1'.

I was looking for the solution but not a single one worked. I'm using python 3.6 and I have set environment variable PYTHONPATH = I:\project\package1

3
  • Are module1.py and module2.py in the same folder? Commented May 25, 2018 at 10:49
  • module1 is in the package1 and module2 is in the package2 Commented May 25, 2018 at 10:53
  • and both package1 and package2 are in the project folder Commented May 25, 2018 at 10:54

1 Answer 1

5

By default, python only searches current directory. So you'll need to append the path a bit.

In module 2:

import sys
sys.path.append('C:\PathTo\project\package1')
import module1

That should fix the issue you were having.

Sign up to request clarification or add additional context in comments.

1 Comment

Late to the show here, but I used this tactic and i tworked for the top level import... but then my import has imports, and I get the same error for the deeper import. Is there no way to solve this in general?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.