How to modularize a Python application

2024/11/17 9:31:34

I've got a number of scripts that use common definitions. How do I split them in multiple files? Furthermore, the application can not be installed in any way in my scenario; it must be possible to have an arbitrary number of versions concurrently running and it must work without superuser rights. Solutions I've come up with are:

  • Duplicate code in every script. Messy, and probably the worst scheme.
  • Put all scripts and common code in a single directory, and use from . import to load them. The downside of this approach is that I'd like to put my libraries in other directory than the applications.
  • Put common code in its own directory, write a __init__.py that imports all submodules and finally use from . import to load them. Keeps code organized, but it's a little bit of overhead to maintain __init__.py and qualify names.
  • Add the library directory to sys.path and import. I tend to this, but I'm not sure whether fiddling with sys.path is nice code.
  • Load using execfile (exec in Python 3). Combines the advantages of the previous two approaches: Only one line per module needed, and I can use a dedicated. On the other hand, this evades the python module concept and polutes the global namespace.
  • Write and install a module using distutils. This installs the library for all python scripts and needs superuser rights and impacts other applications and is hence not applicable in my case.

What is the best method?

Answer

Adding to sys.path (usually using site.addsitedir) is quite common and not particularly frowned upon. Certainly you will want your common working shared stuff to be in modules somewhere convenient.

If you are using Python 2.6+ there's already a user-level modules folder you can use without having to add to sys.path or PYTHONPATH. It's ~/.local/lib/python2.6/site-packages on Unix-likes - see PEP 370 for more information.

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

Related Q&A

Solving for quartile and decile using Python

Is there a library for Python 2.7 that can solve quartiles and deciles. It seems that numpy doesnt have any functions for it. Can you give me a link if there are any. Thanks in advance! :D

Predicted values of each fold in K-Fold Cross Validation in sklearn

I have performed 10-fold cross validation on a dataset that I have using python sklearn, result = cross_val_score(best_svr, X, y, cv=10, scoring=r2) print(result.mean())I have been able to get the mean…

What does the comma mean in Pythons unpack?

We can simply use: crc = struct.unpack(>i, data)why do people write it like this: (crc,) = struct.unpack(>i, data)What does the comma mean?

How do I manually add more cookies to a session which already has cookies set in mechanize?

I have a python script which scrapes a page and receives a cookie. I want to append another cookie to the existing cookies that are being send to the server. So that on the next request I have the cook…

python generators time complexity confusion

I have been reading about keyword yield and generators in python and I want to know if I have understood it right it terms of time complexity.Here is my generator function to get factors:def calc_facto…

Python: Find Amount of Handwriting in Video

Do you know of an algorithm that can see that there is handwriting on an image? I am not interested in knowing what the handwriting says, but only that there is one present? I have a video of someone…

Include nonce and block count in PyCrypto AES MODE_CTR

Some background information, you can skip this part for the actual questionthis is my third question about this topic here at stackoverflow. To be complete, these are the other questions AES with crypt…

Why is pandas.series.map so shockingly slow?

Some days I just hate using middleware. Take this for example: Id like to have a lookup table that maps values from a set of inputs (domain) values, to outputs (range) values. The mapping is unique. A …

Viewset create custom assign value in Django Rest Framework

Would like to set a CustomUsers username by using the input email, but where to do the custom assigning, in view? At the same time it receiving a file as well.Models.pyclass CustomUser(AbstractUser):a…

Remove a relation many-to-many (association object) on Sqlalchemy

Im stuck with a SqlAlchemy problem.I just want to delete an relation. This relation is made by an association object.modelsclass User(db.Model, UserMixin):id = db.Column(db.Integer, pr…