$PYTHONPATH and sys.path don't seems to match

6,137

sys.path in Python is a "list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default." (taken from the Python online documentation).

For sys.path to be affected by PYTHONPATH, the latter has to be an environment variable. If it's not an environment variable but just a shell variable, it will not be visible by the python interpreter. You turn it into an environment variable by exporting it after setting its value, or at the same time, as in

export PYTHONPATH=/some/path:/goes/here
Share:
6,137

Related videos on Youtube

snoob dogg
Author by

snoob dogg

I do a bit of all, I like Max/MSP Jitter env, Python, Javascript, QB45

Updated on September 18, 2022

Comments

  • snoob dogg
    snoob dogg over 1 year

    I want to add a folder permanently to sys.path but it doesn't work.

    On my Debian Stretch, when I execute :

    $ echo $PYTHONPATH
    

    it looks empty

    In the same time, when I print the content of sys.path, there is 10 different paths which aren't the same than those contained into $PATH (and obviously in $PYTHONPATH). How can it be explained ?

    (I had to create a virtualenv for a framework but I don't think I'm using it right now so it's probably not related but I'm not confortable with that concept so maybe I miss something)

    Also I've tried to modify .bashrc to add a path to $PYTHONPATH so I've know the following line :

    PYTHONPATH=$PYTHONPATH:~/bin 
    

    After rebooting, it works, I mean $PYTHONPATH contains it but nothing change for sys.path.

    Also, $ env | grep ^PYTHON returns nothing.

    So, how sys.path is filled and how can I add permanently a folder to it ?

    • snoob dogg
      snoob dogg almost 6 years
      I know that there aren't related but, in the same time, I can't figure out where that list of folder comes from. I thought I was clear about the issue, I can't add a folder to sys.path by modifying $PYTHONPATH.
    • Kusalananda
      Kusalananda almost 6 years
      Did you export PYTHONPATH?
  • snoob dogg
    snoob dogg almost 6 years
    By the way, since my PYTHONPATH was empty of have you an idea, how does the other folders paths were determined ?
  • Kusalananda
    Kusalananda almost 6 years
    @snoobdogg It's dependent on the installation of Python. For a slightly longer explanation, see here: stackoverflow.com/a/38403654/931027