sys path modification
Cameron Simpson
cs at zip.com.au
Mon Jul 27 18:55:00 EDT 2015
On 27Jul2015 14:09, neubyr <neubyr at gmail.com> wrote:
>Modules are installed, but they are in a different directory than standard
>modules directory. I considered an option to add a site specific directory,
>but I want to make module path application specific rather than installing
>it in system-wide directory. virtualenv is one option, but that means
>anyone who wants to run this particular script will need to activate
>virtualenv each time.
Personally I almost always recommend virtualenv over installing in the system
areas - the system package manager can fight with you there.
Rather than forcing users to activate the virtualenv, instead provide a trivial
wrapper script which activates the virtualenv and runs their command, and tell
them about that, not the direct path to the app:
#!/bin/sh
. /path/to/virtualenv/bin/activate
exec python /path/to/app ${1+"$@"}
If you stick that in (say) /usr/local/bin with a sensible name they need never
care.
>I thought allowing application to find/use it's
>dependencies would be easier. Are there any other options?
Well, I actually have an app which imports from a separate location in order to
support user supplied functions that plug into the app. I wrote an
"import_module_name" function to support this:
https://bitbucket.org/cameron_simpson/css/src/tip/lib/python/cs/py/modules.py
which optionally installs a different sys.path for the duration of the import
operation, then reinstalls the old one again. You can even "pip install
cs.py.modules", in theory.
Then you could go:
import_module_name('your.app.modulename', 'YourAppClass', ['path/to/your/app/lib'] + sys.path)
app = YourAppClass(...)
app(args,...)
to suck in the app, instantiate it, and run it. Or some obvious variation on
that.
There are two caveats I'd provide if you go this route:
Since the stdlib's importlib provides no overt exclusion, if you have other
imports taking place in other threads when you run this it is possible that
they may import while your modified sys.path is in place.
Any time you pull from an exterior place, you need to be sure the exterior
place doesn't have unexpected code - if someone untrusted can insert python
code into the extra lib path then they may get it imported.
Cheers,
Cameron Simpson <cs at zip.com.au>
More information about the Python-list
mailing list