views:

186

answers:

2

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?

A: 

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

profjim
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.
l0b0
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.
profjim
+1  A: 

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

jcoon
edit.py and check.py are not in the same directory.
l0b0
please update your question - it states they are in site-packages/progam_name/check.py and site-packages/program_name/edit.py, correct?
jcoon
If you add `modules/check/lib/` and `modules/edit/lib/` to your PYTHONPATH, you can call `import check` and `import edit`
jcoon
I can't change the import syntax, at least for now. The software is developed by 10+ persons.
l0b0
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
jcoon
Changing to `import check` would probably go down nicely, but how do you structure the source directories then? We can't just put all the source files in the same directory.
l0b0
You can keep them as is as long as you add each source directory to your PYTHONPATH in your development enviornment - that is a standard use of the PYTHONPATH variable, so you can link to other source directories.
jcoon
OK, thanks. I'll give you a point anyway, but do you think this is doable with relative imports instead of PYTHONPATH?
l0b0
Since your development structure doesn't match the installation structure, the only clean way to handle it is through PYTHONPATH.
jcoon