Oblomov
Oblomov

Reputation: 9655

Module not found error because path is interpreted as module

I have downloaded a package with the following general structure:

 - pkg
   - runs
     - __init__.py
     - script.py
   - data
     - subdata
       - __init__.py
       - datascript.py

When I try to run script.py from the Anaconda prompt using

C:\pkg>python runs/script.py

I get the error

Traceback (most recent call last):
  File "runs/script.py", line 4, in <module>
    from data.subdata import *
ModuleNotFoundError: No module named 'data.subdata'

so apparently, python is interpreting data.subdata as a module instead of a path.

How can I fix that?

Upvotes: 1

Views: 1696

Answers (1)

Roushan
Roushan

Reputation: 4440

add the path in sys.path ->sys.path.append('path_to_module')

  • import statements search through the list of paths in sys.path
  • sys.path always includes the path of the script invoked on the command line and is agnostic to the working directory on the command line.
  • importing a package is conceptually the same as importing that package’s init.py file

read more :
https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html

Upvotes: 1

Related Questions