Python Forum
Add file to sys.path permanently
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add file to sys.path permanently
#1
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?
Reply
#2
https://linuxize.com/post/how-to-add-dir...-in-linux/
Reply
#3
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.
ndc85430 and ibreeden like this post
Reply
#4
(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?
Reply
#5
I think you can create the directory by yourself if they don't exist.
Reply
#6
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Subprocess.Popen() not working when reading file path from csv file herwin 13 1,160 May-07-2021, 03:26 PM
Last Post: herwin
  PyDrive download file path MiniMinnow 0 1,475 Apr-28-2020, 03:01 PM
Last Post: MiniMinnow
  String to File Path creedX 4 1,052 Apr-06-2020, 07:29 PM
Last Post: creedX
  Add path to a local file in pop-up field pythonscripting 1 698 Feb-08-2020, 10:57 PM
Last Post: Larz60+
  How to get file name without the full path details and without extension aruncom2006 1 2,821 Jan-13-2020, 07:37 AM
Last Post: Larz60+
  How to include Variable in File Path penahuse 3 3,053 Jan-05-2020, 03:08 AM
Last Post: ichabod801
  How to search full path of specified file SriRajesh 3 1,023 Dec-14-2019, 04:52 PM
Last Post: Gribouillis
  how do i write a cross-platform relative file path? pseudo 2 1,102 Aug-23-2019, 05:07 PM
Last Post: ThomasL
  File Path not recognised starzar 3 1,461 Jul-27-2019, 05:23 AM
Last Post: starzar
  Best way of taking a date prefix from a line and forming a file path from it? Skaperen 5 1,164 Jul-22-2019, 11:50 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020