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.

2
  • Thankyou @Madhuri Patel for correcting the spelling Commented Jan 2, 2020 at 13:11
  • @scicyb thank you, is that a file in the same dir that my python file I'm running is in? Commented Jan 2, 2020 at 13:14

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

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)?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.