Search code examples
pythoneclipsepydev

Run Ecplise/PyDev project from command line


I'm developing a system in Python that includes a calculation engine and a front end. I've split them up into two projects as the calculation engine can be used for other front ends as well.

I'm using Eclipse and PyDev. Everything works perfectly in Eclipse/PyDev, but as soon as I try to run it outside of PyDev (from command line) I get importing errors. I've done quite a bit of research to find the problem, but I just don't see a solution that works nicely. I believe that PyDev modifies the Python path.

In my project layout below I have two packages (package1 and tests) within one project (Calculations). I can't seem to import anything from package1 in tests. I also have another project (Frontend). Here I also can't import anything from package1.

What I want to understand is the proper way of calling my script/tests files from the command line? Both for two separate projects and two packages in the same project. I assume it would be similar to how PyDev does it. So far I think I have the following options:

  • Create python code to append to sys.path (seems hacky/not good practice)
  • Modify the PYTHONPATH when I call the test_some_calc.py like this: PYTHONPATH= python test_some_calc.py (I think this is how PyDev does it, but it seems a bit long - there must be a simpler way?
  • Make a install package (eventually I might go this method, but not yet.)

I have the following project layout.

CodeSolution/
    Calculations/
       package1/
           __init__.py
           subpackage/
               __init__.py
               some_calc.py
           subpackage2/
               __init__.py
               another_calc.py
       tests/
           __init__.py
           subpackage/
               __init__.py
               test_some_calc.py  # Unable to import from package1
           subpackage2/
               __init__.py
               test_another_calc.py  # Unable to import from package1
    Frontend/
        some_script.py  # Unable to import from package1

Comments on my project layout will also be appreciated.


Solution

  • A clean, quick and modular way to include certain python from anywhere on your system is to make a file named mymodule.pth and put it inside the path of site-packages

    mymodule.pth should have the path of your project. Project folder must have an __init__.py file.

    for example put:

    for Linux:

    /home/user/myproject

    inside

    /usr/lib/python2.7/site-packages/mymodule.pth

    or

    for Windows

    C:\\Users\myUsername\My Documents\myproject

    inside

    C:\PythonXY\Lib\site-packages\mymodule.pth