best way to ensure './' is at beginning of sys.path?

Cameron Simpson cs at zip.com.au
Fri Feb 3 23:58:06 EST 2017


On 04Feb2017 13:59, Steve D'Aprano <steve+python at pearwood.info> wrote:
>On Sat, 4 Feb 2017 01:13 pm, Cameron Simpson wrote:
>
>>>1. In your .bashrc file, or equivalent, set the environment
>>>   variable PYTHONPATH:
>>>export PYTHONPATH='./;$PYTHONPATH'
>>
>> You want double quotes (allowing parameter substitution) instead of single
>> quotes here. Or, of course, no quotes at all. And the separator is ":",
>> not ";".
>
>Thanks for the correction.
>
>> Personally, I'm against hacking the $PYTHONPATH this way at all.
>
>Oh? Why's that? This is the whole purpose of PYTHONPATH.

I'm only against doing it "this way", for values of "this way" including 
"putting a ., which is totally dependent on my working directory, in my 
$PYTHONPATH".  I'm sorry I wasn't clear.

>> Far better to invoke the Python script via a shell script that includes
>> the absolute path of the current directory (or the module directory) in
>> the $PYTHONPATH.
>
>For some definition of "better" -- that means you have double the cognitive
>load. For every script.py you want, you need to create both script.py (what
>you actually want) and script.sh (a wrapper that sets the environment).

Sure, but (I hope) Neal's need is a little special purpose.

Besides, when i do this with "installed" code, generally all the python is in a 
module in my normal $PYTHONPATH and the shell script, if it exists (eg to hack 
the $PYTHONPATH) looks a bit like this:

  #!/bin/sh
  set -ue
  PYTHONPATH=/path/to/extra/libs:$PYTHONPATH
  export PYTHONPATH
  exec python -m my.module ${1+"$@"}

So you don't really install 2 scripts. You install a python module in your 
normal place, and the invoker is a shell script which might prehack the 
environment.

My "dev" script is little different (though it mucks with $PATH and $PERL5LIB 
etc as well as $PYTHONPATH), but its whole purpose is to reduce my cognitive 
load, which just becomes "I want this directory as the basis for my dev env for 
this one command".

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list