0

I have a solution with different folders and I wanted to create a common package for them to share reusable code. There are lot of examples for python packages are there. None of them addresses separate folder scenario in a simple way.

My folder structure is :

Client/
 __init__.py
 --logic.py
Common/
 __init__.py
 -- Constants.py
Server/
 __init__.py
 -- Test.py

My Test.py looks like this :

from Common import Constant 
print(Constant.TEST_VALUE) #-- From common package

But the code not even passes the import statements.

Exception has occurred: ModuleNotFoundError No module named 'Common'

If I try with relative path : from ..Common import Constant

Error:

Exception has occurred: ImportError attempted relative import with no known parent package

My purpose is to reuse the constant.py file for both Cars and Server solution. There are examples for sys.path which I am not sure if its a good practice. Also the code in Common will not be having complex codes to proceed with installing them.

asked Feb 2, 2022 at 16:52
7
  • "I would like to use the Common module and Cars module in Test.py" - those aren't modules; they're packages. "None of them addresses separate folder scenario in a simple way." Right; normally a project has a single top-level package. The simple solution is to just make one that encloses your other three folders, and then follow the usual advice. If you really do want separate packages that interact with each other, then the simplest approach is to install them (ideally in a virtual environment). Commented Feb 2, 2022 at 16:56
  • "OR PATH is used to specify the path to module." I think you mean PYTHONPATH. I generally recommend against this. Commented Feb 2, 2022 at 16:56
  • Does my general answer here to a different importing-related question happen to resolve the issue for you? Commented Feb 2, 2022 at 16:58
  • @Karl In my case this is like a client-server arch,. One solution will be Cars+Common and another is Server+Common. I am from .NET background, that is why these mistakes. Commented Feb 2, 2022 at 17:06
  • 1
    Aha, so some users would specifically want two but not all three packages? Then I suppose you really do have three projects in your solution (in VS-speak), where Cars and Server both depend on Common. Best treat them as separate packages, and thus use absolute imports between them. Commented Feb 2, 2022 at 17:11

2 Answers 2

1

If these packages are meant to be part of the same conceptual project, then simply make a containing package. Now the relative import will work as long as you start from outside the package hierarchy, and ensure the containing folder is on sys.path. The easy ways to do that are to install the package (preferably to a virtual environment), or start python from that folder (in which case Cars, Common and Server appear as separate packages) or just outside (in which case the containing folder is a package and Cars, Common and Server are sub-packages - which is probably what you want; it's more consistent with how you will most likely install the code later).

If these packages are separate projects that happen to depend upon one another, then Python has no reason by default to look in any particular place for the dependency packages - aside from the places it ordinarily looks for anything. So, again, you can start python from the containing folder if there actually is such a containing folder; but there isn't a good project-organization reason for there to be a containing folder - so set up an actual project for each package, and install them all to the same virtual environment. Now you can do cross-package imports with absolute syntax (but not relative syntax, because that only works within a package hierarchy).

answered Feb 2, 2022 at 17:09
Sign up to request clarification or add additional context in comments.

1 Comment

I assume that in the previous research you mention, you saw how to set up packages for installation, and install them. If not, this is the authoritative reference, straight from python.org.
0

You can use this code found here:

# Test.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/Cars/')
sys.path.insert(1, '/path/to/Common/')
import Audi
import Constants
answered Feb 2, 2022 at 16:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.