Python Forum
Add file to sys.path permanently - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Add file to sys.path permanently (/thread-32255.html)



Add file to sys.path permanently - hcccs - Jan-30-2021

I'm running Python 3.7.3 and trying to add a library to sys.path. I can add it but it doesn't stick after next reload. I edited ~/.bashrc with the following:
PYTHONPATH="${PYTHONPATH}:/home/pi/Code"
export PYTHONPATH
PYTHONPATH now contains just '/home/pi/Code'. I have added an empty file '__site__.py' to the library. Is there something missing?


RE: Add file to sys.path permanently - Axel_Erfurt - Jan-31-2021

https://linuxize.com/post/how-to-add-directory-to-path-in-linux/


RE: Add file to sys.path permanently - Gribouillis - Jan-31-2021

DO NOT USE PYTHONPATH. Instead do this:
  1. Find the per user site-packages directory. For this, start python and run
    >>> import site
    >>> site.getusersitepackages()
    On the computer where I'm writing this, it prints '/home/eric/.local/lib/python3.6/site-packages'. This is the path to the directory
  2. In this directory, create a file named usercustomize.py. In this file add code
    # usercustomize.py
    import sys
    sys.path.extend(['/home/pi/Code',])
You can add more directories to extend sys.path automatically this way. This is by far the most flexible way to do this. PYTHONPATH is a plague because if you install several python interpreters on your computer, it will apply to all of them and this is not what you want.

Edit: Sorry, it is usercustomize and not sitecustomize. The latter also exists but it is usually stored in the global site-packages directory.


RE: Add file to sys.path permanently - hcccs - Jan-31-2021

(Jan-31-2021, 10:55 AM)Gribouillis Wrote: DO NOT USE PYTHONPATH. Instead do this:
  1. Find the per user site-packages directory. For this, start python and run
    >>> import site
    >>> site.getusersitepackages()
    On the computer where I'm writing this, it prints '/home/eric/.local/lib/python3.6/site-packages'. This is the path to the directory
  2. In this directory, create a file named usercustomize.py. In this file add code
    # usercustomize.py
    import sys
    sys.path.extend(['/home/pi/Code',])
You can add more directories to extend sys.path automatically this way. This is by far the most flexible way to do this. PYTHONPATH is a plague because if you install several python interpreters on your computer, it will apply to all of them and this is not what you want.

Edit: Sorry, it is usercustomize and not sitecustomize. The latter also exists but it is usually stored in the global site-packages directory.

Gribouillis
site.getusersitepackages() gives me /home/pi/.local/lib/python3.7/site-packages but when I go into Unix shell to create the usercustomize.py there is no directory 'lib' in '.local', just a 'share' directory. Should it be done in python shell?


RE: Add file to sys.path permanently - Gribouillis - Jan-31-2021

I think you can create the directory by yourself if they don't exist.


RE: Add file to sys.path permanently - hcccs - Jan-31-2021

(Jan-31-2021, 11:10 AM)Gribouillis Wrote: I think you can create the directory by yourself if they don't exist.

I did and removed the contents of PYTHONPATH and reloading the sys.path contains my library. The strange thing is I've been searching the web for hours and got different ideas how to implement this and they were obviously all wrong.
Thanks! Smile