sys path modification

Cameron Simpson cs at
Tue Jul 28 00:55:00 CEST 2015

On 27Jul2015 14:09, neubyr <neubyr at> 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:

  . /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 

>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:

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", in theory.

Then you could go:

  import_module_name('', 'YourAppClass', ['path/to/your/app/lib'] + sys.path)
  app = YourAppClass(...)

to suck in the app, instantiate it, and run it. Or some obvious variation on 

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.

Cameron Simpson <cs at>

More information about the Python-list mailing list