0

I get the module not found error when importing my self made module.

Structure:

environment (folder/module to import)
project (folder)
-- project_scripts (folder)
   -- main_script.py

The problem is that I can run the main_script.py file from my text editor (pycharm) but not from the command line, then it gives the moduleNotFound error: no module named environment. Which is something I want to realize: running from the terminal.

The weird thing is that Pycharm gives type hints for the module, so it seems to recognize it. I am using an anaconda environment, but I made sure to activate it (in the terminal) and have set it as the Pycharm interpreter.

Details: Python 3.8.16 The module has a init file

I hope someone could help me in the right direction, as I have been struggling more than 1 times with importing self-made modules to Python files, and struggled to find a consistent method.

I feel like it might have something to do with PYTHONPATH but im unsure as I don't have too much knowledge about this.

I tried different methods, such as changing the cwd to a different place (thought it would find the module in a different area) but that didn't seem to work.

R.Luinge
  • 25
  • 4

1 Answers1

0

when you launch python, it will include the current directory in its path and look for modules within and under it. I assume you are running your script using python main_script.py from the project_scripts folder? try running it from the environment folder as python project/project_scripts/main_script.py.

also, there are more SO questions regarding relative imports, see How can I do relative imports in Python? and Importing from a relative path in Python