0

First, imagine the following file structure:

project-dir
|_ 
   package1
   |_ 
      __init__.py
      module1.py
      module2.py

And the following script contents:

module2.py:

def func_module2():
    print('func_module2 run')

module1.py:

from package1 import module2

module2.func_module2()

The following command creates an error when executed from the project-dir:

python package1/module1.py

Error:

Traceback (most recent call last):
  File "./package1/module1.py", line 1, in <module>
    from package1 import module2
ImportError: No module named 'package1'

Why is this happening and how is it possible to run a Python script from another directory?

Python 3.5.2

7
  • Try stackoverflow.com/questions/31510944/… Commented Feb 24, 2017 at 14:14
  • Can you do ls to check if you are in the project-dir directory. Commented Feb 24, 2017 at 14:14
  • Yes I am indeed inside the project dir. You can tell from the error that module1.py is actually executed. The problem seems to be the import which is not handled properly for whatever reason. Commented Feb 24, 2017 at 14:19
  • 1
    You can also run it with this command: python -m package1.module1. Commented Feb 24, 2017 at 14:22
  • What is the python version? Commented Feb 24, 2017 at 14:41

2 Answers 2

0

module2 and module1 are under the same namespace. So it MUST be:

# module1.py
import module2

module2.func_module2()

from package1 import module2 is valid only in the following context/namespace:

enter image description here

Exemple:

# test.py
from package1 import module2

module2.func_module2()
Sign up to request clarification or add additional context in comments.

5 Comments

Please don't recommend implicit relative imports EVER. By the fault, if not using the explicit relative import syntax, the imports in next versions of Python are absolute, so using implicit relative imports is highly discouraged
It won't let me import module2 in module1. Pycharm error is No module named module2. My file structure is exactly the one from your screenshot.
As I said, relative implicit imports (import module2.py # @ module1.py) are deprecated. Use either explicit relative imports or absolute ones.
@Adirio Are you sure? I had not ever listen about that. Can you provide us an official source for further info?
Of course: Third bullet from PEP8 (python.org/dev/peps/pep-0008/#imports)
0

Your problem seem to be that module1.py and module2.py both lives in the same package. From module1.py you won't find module2.py in another package. Instead try:

import module2

module2.func_module()

Trying that I get:

> python package1/module1.py
func_module2 run

However if you want it to work as a package too (or using newer versions of python), you must istead use

from . import module2

module2.func_module()

but that will make python package1/module1.py not work since it will not be run as part of a package. Instead you can use

> python -m package1.module1
func_module2 run

8 Comments

import module2 won't work. from . import module2 will import the module properly. Running the script with this configuration generates another error: SystemError: Parent module '' not loaded, cannot perform relative import
@user1211030 Have you tried? On my computer that works. However your suggestion from . import module2 does not work as it will raise ValueError: Attempted relative import in non-package. Note that by running python package1/module1.py it will not be considered to be in a package despite the __init__.py file.
Please don't recommend implicit relative imports EVER. By the fault, if not using the explicit relative import syntax, the imports in next versions of Python are absolute, so using implicit relative imports is highly discouraged.
@skyking Yes I have tried. Which version of Python are you running?
python -m package1.module1 works well, but I can't accept this answer if it includes suggesting import module2.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.