Search code examples
pythonpython-3.ximportpycharmdirectory-structure

Python folder structure for project directory and easy import


My team has a folder of several small projects in python3. Amongst them, we have a utility folder with several utility functions, that are used throughout the projects. But the way to import it is very uncomfortable. This is the structure we use:

temp_projects
    util
        storage.py
        geometry.py
    project1
        project1.py
    project2
        project2.py

The problem is that the import in the projects looks terrible:

sys.path.insert(1, os.path.join(sys.path[0], '..'))
import util.geometry

util.geometry.rotate_coordinates(....)

Also, pycharm and other tools are having trouble understanding it and to supply completion.

Is there some neater way to do that?

Edit: All of the projects and utils are very much work-in-progress and are modified often, so I'm looking for something as flexible and comfortable as possible


Solution

  • PYTHONPATH environment variable might be a way to go. Just set it to projects folder location:

    PYTHONPATH=/somepath/temp_projects
    

    and you'll be able to use util as follows:

    import util.geometry
    
    util.geometry.rotate_coordinates(....)
    

    Also this will be recognized by PyCharm automatically.