3

I'm working on a project where all the code in the source tree is separated into module directories, e.g.:

modules/check/lib/check.py
modules/edit/lib/edit.py

During installation, the Python files are put in the same directory program_name under Python's site-packages. All the modules therefore use the syntax import program_name.edit.

Because of the directory and import structure, the source modules are unable to import each other, so you'd have to install them each time you want to run anything in the source tree.

My questions are therefore: Without modifying the directory structure, how can I make sure that modules/check/lib/check.py imports from modules/edit/lib/edit.py and that site-packages/program_name/check.py imports from site-packages/program_name/edit.py? And for a possible reorganization, what are best practices for the directory structure and imports in an environment like this?

2 Answers 2

2

You can just add the /modules/ directories to your PYTHONPATH in your dev environment. Once installed in site-packages, calling import edit inside check.py will import the correct module since they are in the same directory. Calling import edit from your dev environ will import the one you added to your PYTHONPATH

Sign up to request clarification or add additional context in comments.

8 Comments

please update your question - it states they are in site-packages/progam_name/check.py and site-packages/program_name/edit.py, correct?
If you add modules/check/lib/ and modules/edit/lib/ to your PYTHONPATH, you can call import check and import edit
I can't change the import syntax, at least for now. The software is developed by 10+ persons.
You should propose changing the syntax to your team, calling import program_name.check is not neccessary, and also happens to be making development difficult. I think that is a small refactor compared to changing directory structures
Since your development structure doesn't match the installation structure, the only clean way to handle it is through PYTHONPATH.
|
0

Why don't you install symlinks underneath prog_name on your dev machine?

2 Comments

Then I'd have to maintain symlinks to each Python file in the source tree. Also, I'd like to be able to use the current build process to get the latest updates in site-packages at my discretion, not whenever I update something in the source tree.
So if I understand all your comments correctly, (1) you don't want the source versions of these modules visible to the rest of your Python installation---only the last installed version of them. Also (2) you don't want to change the import syntax in the source files, they should use the same import syntax (and with no conditional imports, I'm supposing) that they use when installed. Nonetheless (3) the source modules should import the source versions of each other. I don't think (1)-(3) are jointly satisfiable.

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.