ModuleNotFoundError - Import module from specific directory

时间:2017-12-17 08:24:41

标签: python python-3.x

I have the following folder structure for my Python project (in Pycharm):

Tool
|___Script
      |___logs
      |___ tests
      |    |___setA
      |    |___setB
      |    |___setC
      |         |___testSetC.py
      |___ __init__.py
      |______script.py

and I'm trying to import methods defined in script.py in testSetC.py.

I have from Script.script import * at the top of testSetC.pyand the script runs without any issues inside Pycharm. However, when I tried to run the same from the command line, it failed with the error message ModuleNotFoundError: No module named 'Script'.

I poked around and found that Pycharm's run configuration automatically adds the Tool directory to PYTHONPATH essentially masking the error.

What is the correct way to do this without manually amending sys.path in every test script? It looks hackish IMO.

0 个答案:

没有答案