Running a Script

    TLDR πŸ”—

    Use the following (Sets PYTHONPATH in addition to normal run command)

    PYTHONPATH=$(pwd) python src/main.py
    

    Background πŸ”—

    At first blush this seems obvious and very easy but… I inherited a setup on my first big python project that had the code inside a src folder and data and other things in other folders at that level. It expected all commands to be executed from that folder. All paths in the code were relative from that working directory. While it make the code harder to run from the command line it wasn’t any harder in PyCharm (other than setting the correct working directory). Overall I liked the separation of data from code but co-location so carried it over to a few other projects. But each time I need to run it from the command line I run into issues so documenting how to run it for future reference. Running from the correct folder is not enough to make it work hence why I’m writing it down.

    Example πŸ”—

    Example folder tree πŸ”—

    $ tree example/
    example/
    β”œβ”€β”€ data
    β”‚Β Β  β”œβ”€β”€ file1.csv
    β”‚Β Β  └── file2.csv
    └── src
        β”œβ”€β”€ helper.py
        └── main.py
    
    3 directories, 5 files
    

    Content of src/main.py πŸ”—

    $ cat src/main.py 
    from src.helper import hello_again
    
    print("Hi from main")
    hello_again()
    

    Content of src/helper.py πŸ”—

    $ cat src/helper.py 
    def hello_again():
    	print("Well hello there")
    

    Error when trying from inside the example folder πŸ”—

    $ python src/main.py
    Traceback (most recent call last):
    File "/home/user/example/src/main.py", line 1, in <module>
    from src.helper import hello_again
    ModuleNotFoundError: No module named 'src'
    

    Successful command by setting PYTHONPATH to current directory πŸ”—

    $ PYTHONPATH=$(pwd) python src/main.py
    Hi from main
    Well hello there