0

My structure looks like this:

main_dir
- code_1.py
- sub_dir
    - code_2.py
-__init__.py

Now in code_1.py I define a class (a custom OpenAi Gym Env for that matter) that I want to use in my code_2.py. When using PyCharm everything works just fine, I can import the class like this

from main_dir.code_1 import GymEnvClass

I do not want PyCharm to be a requirement to execute the scripts (in this example code_2.py), so it should just run from the terminal.

I tried setting a __init__.py in the main_dir or relative imports like from ..code_2 import GymEnvClass but when I try to run it from terminal it always fails ModuleNotFound: No Module Named main_dir or ImportError: attempted relative import with no known parent package

PonyTale
  • 306
  • 1
  • 7
  • 1
    Similar question: https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder – cs1349459 Mar 29 '22 at 17:11
  • I found a solution with appending the parent-dir to sys.path and importing inside of __init__.py . PyCharm now shows a red line indicating an error. It works but I do not really like the way this is handled. I still wonder how PyCharm is able to find the correct files .. – PonyTale Mar 30 '22 at 13:54

0 Answers0