0

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.

Madhuri Patel
  • 1,270
  • 12
  • 24
Jachdich
  • 782
  • 8
  • 23

1 Answers1

1

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
bastantoine
  • 582
  • 4
  • 21
  • 1
    Hang on a sec, doesn't seem to be working: `ModuleNotFoundError: No module named 'lib'`. Do I need to add the `lib` dir to my PATH (or more specifically my sys.path)? – Jachdich Jan 02 '20 at 13:24
  • Well I think you need to add either the `lib` folder or the entire folder of your project to the `sys.path` so that Python would be able to search for the `lib` module. – bastantoine Jan 02 '20 at 14:37