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.
ibreeden and ndc85430 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
  save values permanently in python (perhaps not in a text file)? flash77 8 1,121 Jul-07-2023, 05:44 PM
Last Post: flash77
  File path by adding various variables Mishal0488 2 968 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Script File Failure-Path Error? jerryf 13 3,315 Nov-30-2022, 09:58 AM
Last Post: jerryf
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,150 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Subprocess.Popen() not working when reading file path from csv file herwin 13 14,622 May-07-2021, 03:26 PM
Last Post: herwin
  PyDrive download file path MiniMinnow 0 3,210 Apr-28-2020, 03:01 PM
Last Post: MiniMinnow
  String to File Path creedX 4 3,214 Apr-06-2020, 07:29 PM
Last Post: creedX
  Add path to a local file in pop-up field pythonscripting 1 1,621 Feb-08-2020, 10:57 PM
Last Post: Larz60+
  How to get file name without the full path details and without extension aruncom2006 1 5,850 Jan-13-2020, 07:37 AM
Last Post: Larz60+
  How to include Variable in File Path penahuse 3 7,221 Jan-05-2020, 03:08 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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