0

I am having the error shown in title. The context is this:

  • MainDir
    • PackageDir
      • __init__.py
      • otherpython.py
    • ScriptDir
      • script.py (which imports Package)

If I go in a terminal while in MainDir and run python ScriptDir/script.py I get the error in the title, that is that my package is not found. If, however, I open a python interpreter from the MainDir, and then import the package, the package is found without issues.

MainDir > python
>>> import Package     #No problems

Note that PYTHONPATH contains the PackageDir. When checking the sys.path, the output contains the absolute path to the PackageDir, that is

sys.path = ['/home/user/MainDir/PackageDir','PathToAnaconda',...]

is part of the outputed list.

I am very confused what is going on here, could anyone help me? Thank you

Dominus
  • 808
  • 11
  • 25

1 Answers1

0

I am assuming by import package you are trying to access something from PackageDir

Can you try this in your script.py

import sys
sys.path.insert(0,"/MainDir/PackageDir")  

Someone asked something similar in the below link.

Here is a clear answer for the same importing-files-from-different-folders

jiraiya
  • 28
  • 9
  • It does work, but there's something I don't get. That command adds the relative path to the sys.path (/MainDir/PackageDir), but I already have the absolute version of the path in it (/home/user/MainDir/PackageDir). Why would I need to have the relative too? Is python that bad at managing packages? – Dominus Jan 30 '20 at 09:54
  • can you show how absolute version of path is mentioned in your case? – jiraiya Jan 30 '20 at 10:18
  • I've edited the question to make it clearer, let me know if that helps – Dominus Jan 30 '20 at 11:12