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:
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.__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.sys.path
and
import
. I tend to
this, but I'm not sure whether
fiddling with sys.path
is nice code.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.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?
Python scripts are modularized through functions, modules, and packages. Functions allow developers to reduce repetition in their code by executing the same block of code multiple times in one program. By giving the function a name, it can be reused by referencing it's name instead of rewriting the code.
And when it comes to reusing code in Python, it all starts and ends with the humble function. Take some lines of code, give them a name, and you've got a function (which can be reused). Take a collection of functions and package them as a file, and you've got a module (which can also be reused).
Modularity refers to the concept of making multiple modules first and then linking and combining them to form a complete system (i.e, the extent to which a software/Web application may be divided into smaller modules is called modularity).
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.
You can set the PYTHONPATH
environment variable to the directory where your library files are located. This adds that path to the library search path and you can use a normal import
to import them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With