Split Python source into separate directories?

2024/9/16 23:07:19

Here are some various Python packages my company "foo.com" uses:

com.foo.bar.web
com.foo.bar.lib
com.foo.zig.web
com.foo.zig.lib
com.foo.zig.lib.lib1
com.foo.zig.lib.lib2

Here's the traditional way to store the source on disk:

pysrc/com/foo/bar/web/lib/zig/web/lib/lib1/lib2/PYTHONPATH=pysrc

But for organizational purposes (different teams, different revision control, etc.), we want to store these as follows:

bar/pysrc/com/foo/bar/web/lib/
zig/pysrc/com/foo/zig/web/lib/lib1/lib2/PYTHONPATH=bar/pysrc:zig/pysrc

The question is:

Are there any issues with this second method of organization?

For example, if we import com.foo, where would Python look for the __init__.py?

Would symlinking these directories make sense? e.g.:

pysrc/com/foo/bar/ -> symlink to /bar/pysrc/com/foo/zig/ -> symlink to /zig/pysrc/com/foo/

Any general code organizational suggestions are welcome.

Answer

Python will go through sys.path in order (which includes PYTHONPATH and then some), looking for a com.foo package in each. The first one it finds, it will use to the exclusion of others, unlike Perl or Java which effectively merges together package namespaces. There are things you can do to __path__ that change this behavior, but "first match wins" is how Python behaves out of the box.

As long as you keep all of com.foo.bar entirely in bar/ and all of com.foo.zig entirely in zig/, you shouldn't have any problems with the second layout.

https://en.xdnf.cn/q/72558.html

Related Q&A

How can I use a raw_input with twisted?

I am aware that raw_input cannot be used in twisted. However here is my desired application.I have an piece of hardware that provides an interactive terminal serial port. I am trying to connect to th…

How to use Python and HTML to build a desktop software?

Maybe my question is stupid but I still want to ask. I am always wondering whether I can use Python, HTML and Css to develop a desktop software. I know there are alrealy several good GUI frameworks lik…

More efficient way to look up dictionary values whose keys start with same prefix

I have a dictionary whose keys come in sets that share the same prefix, like this:d = { "key1":"valA", "key123":"valB", "key1XY":"valC","…

When should I use dt.column vs dt[column] pandas?

I was doing some calculations and row manipulations and realised that for some tasks such as mathematical operations they both worked e.g.d[c3] = d.c1 / d. c2 d[c3] = d[c1] / d[c2]I was wondering wheth…

Quiver matplotlib : arrow with the same sizes

Im trying to do a plot with quiver but I would like the arrows to all have the same size.I use the following input :q = ax0.quiver(x, y, dx, dy, units=xy ,scale=1) But even if add options like norm = t…

How to convert Tensorflow dataset to 2D numpy array

I have a TensorFlow dataset which contains nearly 15000 multicolored images with 168*84 resolution and a label for each image. Its type and shape are like this: < ConcatenateDataset shapes: ((168, 8…

CSV remove field value wrap quotes

Im attempting to write a list to a csv, however when I do so I get wrapper quotes around my field values:number1,number2 "1234,2345" "1235.7890" "2345.5687"Using this code…

Python - Py_Initialize unresolved during compilation

I have statically compiled Python2.7 without any error. To test my build, I use the following snippet: #include "Python.h" int main() {Py_Initialize(); }And I am compiling it like this:$ gcc…

Python download large csv file from a url line by line for only 10 entries

I have a large csv file of the client and shared via a url to download and I want to download it line by line or by bytes and I want to limit only for 10 entries.I have the following code which will do…

Flask-Login still logged in after use logouts when using remember_me

To logout a user in flask using Flask-login, i simply call logout_user(), but after adding some additional checks with session, after I click logout and click back to "login page" again, im s…