How to properly structure internal scripts in a Python project?

后端 未结 7 1255
抹茶落季
抹茶落季 2021-02-14 02:58

Consider the following Python project skeleton:

proj/
├── foo
│   └── __init__.py
├── README.md
└── scripts
    └── run.py

In this case f

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-14 03:53

    Python looks for packages/modules in the directories listed in sys.path. There are several ways of ensuring that your directories of interest, in this case proj, is one of those directories:

    1. Move your scripts to the proj directory. Python adds the directory containing the input script to sys.path.
    2. Put the directory proj into the contents of the PYTHONPATH environment variable.
    3. Make the module part of an installable package and install it, either in a virtual environment or not.
    4. At run time, dynamically add the directory proj to sys.path.

    Option 1 is the most logical and requires no source changes. If you are afraid that might break something, you can perhaps make scripts a symbolic link pointing back to proj?

    If you are unwilling to do that, then ...

    You may consider it a hack, but I would recommend that you do modify your scripts to update sys.path at runtime. But instead append an absolute path so that the scripts can be executed regardless of what the current directory is. In your case, directory proj is the parent directory of directory scripts, where the scripts reside, so:

    import sys
    import os.path
    
    parent_directory = os.path.split(os.path.dirname(__file__))[0]
    if parent_directory not in sys.path:
        #sys.path.insert(0, parent_directory) # the first entry is directory of the running script, so maybe insert after that at index 1
        sys.append(parent_directory)
    

提交回复
热议问题