'Importing a class from parent python file

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source