Running modules as scripts

I need some help running modules as stand alone scripts. Let's assume I have the following folder layout:

  • my_package
    • __init__.py
      • Workers
        • worker_monitor.py
        • worker_process.py
      • WorkersTest
        • __init__.py
        • worker_test.py
        • test_data.dat

In the following directory structure, worker_test.py is the unit test module for worker_monitor and worker_process. Now, I can execute worker_test.py fine from within PyCharm (PyCharm is totally awesome here!) I am struggling to run this test from the command line.

If I run the tests from WorkersTest/, I get the error  "ImportError: No module named my_package.Workers.worker_monitor". I'm assuming that PyCharm successfully runs the tests because it applies some magic to the paths.

I would like to know what magic is being applied? Thanks.

2 comments
Comment actions Permalink

PyCharm adds the root directory of the project to the PYTHONPATH environment variable.

0
Comment actions Permalink

Simple and it works handsomely.

Thanks Dmitry.

0

Please sign in to leave a comment.