Re: sys path modification
On 27Jul2015 14:09, neubyr 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: sys path modification
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. I thought allowing application to find/use it's dependencies would be easier. Are there any other options? On Mon, Jul 27, 2015 at 12:45 PM, Ned Batchelder wrote: > On Monday, July 27, 2015 at 1:24:50 PM UTC-4, neubyr wrote: > > I am trying to understand sys.path working and best practices for > managing it within a program or script. Is it fine to modify sys.path using > sys.path.insert(0, EXT_MODULES_DIR)? One stackoverflow answer - > http://stackoverflow.com/a/10097543 - suggests that it may break external > 3'rd party code as by convention first item of sys.path list, path[0], is > the directory containing the script that was used to invoke the Python > interpreter. So what are best practices to prepend sys.path in the program > itself? Any further elaboration would be helpful. > > The best practice is not to modify sys.path at all, and instead to install > modules you need to import. That way they can be imported without > resorting > to sys.path fiddling in the first place. > > Is there a reason you can't install the modules? Maybe we can help solve > that. > > --Ned. > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: sys path modification
On Monday, July 27, 2015 at 1:24:50 PM UTC-4, neubyr wrote: > I am trying to understand sys.path working and best practices for managing it > within a program or script. Is it fine to modify sys.path using > sys.path.insert(0, EXT_MODULES_DIR)? One stackoverflow answer - > http://stackoverflow.com/a/10097543 - suggests that it may break external > 3'rd party code as by convention first item of sys.path list, path[0], is the > directory containing the script that was used to invoke the Python > interpreter. So what are best practices to prepend sys.path in the program > itself? Any further elaboration would be helpful. The best practice is not to modify sys.path at all, and instead to install modules you need to import. That way they can be imported without resorting to sys.path fiddling in the first place. Is there a reason you can't install the modules? Maybe we can help solve that. --Ned. -- https://mail.python.org/mailman/listinfo/python-list
Re: sys path modification
On Jul 27, 2015, at 1:24 PM, neubyr wrote: > > I am trying to understand sys.path working and best practices for managing it > within a program or script. Is it fine to modify sys.path using > sys.path.insert(0, EXT_MODULES_DIR)? One stackoverflow answer - > http://stackoverflow.com/a/10097543 - suggests that it may break external > 3'rd party code as by convention first item of sys.path list, path[0], is the > directory containing the script that was used to invoke the Python > interpreter. So what are best practices to prepend sys.path in the program > itself? Any further elaboration would be helpful. Why are you trying to modify sys.path? I'm not judging, there are many good reasons to do so, but there may be safer ways of getting the effect you want that don't rely on modifying sys.path. One simple method is to modify PYTHONPATH (https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH) instead. In order of preference: 1) Append to sys.path. This will cause you the fewest headaches. 2) If you absolutely have to insert into the list, insert after the first element. As you noted from SO, and noted in the docs (https://docs.python.org/3/library/sys.html#sys.path), the first element of sys.path is the path to the directory of the script itself. If you modify this, you **will** break third-party code at some point. Thanks, Cem Karan -- https://mail.python.org/mailman/listinfo/python-list