Search code examples
pythonpython-3.xfilesystemsdirectory-structure

Is it possible to have a separate lib and src directory in Python?


I'm writing a program with a client and a server and I'm trying to organise my source files in an intuative way. I've got this rough file structure:

src:  
    client:  
        client.py  
    server:  
        server.py

lib:
    clientlib:
        client_depend.py
    serverlib:
        server_depend.py
    commonlib:
        both_depend.py

Previously, I was using the methods which described in this SO Post but the number of sys.path.append("../..")s has gotten out of hand and is in danger of breaking if I move any files about.

What would be a neat and pythonic way to do this? I've thought about making lib a package and putting it in $(PYTHONDIR)/Lib/site-packages but that adds complexity to development (as it's a root owned dir and it's not on my USB drive that I use for development so I can change computers easily).

Thanks in advance.


Solution

  • Why not making modules that you could import easily when and where you want?

    Like this:

    src:
        __init__.py
        client:
            __init__.py
            client.py
        server:
            __init__.py
            server.py
    
    lib:
        __init__.py
        clientlib:
            __init__.py
            client_depend.py
        serverlib:
            __init__.py
            server_depend.py
        commonlib:
            __init__.py
            both_depend.py
    

    Then in client.py, you would just have to do:

    from lib.commonlib import both_depend
    from lib.clientlib import client_depend