Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

Knowledge Base

Using Pythonpath with our Python Libraries

Adding the library path to startup

Created 31/01/2016 | Last Updated: 09/11/2024

In the Python programming language, the PYTHONPATH and PATH variables are environment variables that help your computer find the programs and modules it needs to run.

 

PATH Variable

The PATH variable is a list of directories where your operating system looks for executable programs. When you type a command in the terminal, the system checks the directories listed in PATH to find the program to run.

For example, when you type python to start Python, the system looks through the directories in PATH to find the Python executable. If Python is installed and its directory is included in PATH, it will start Python. Otherwise, you will get an error saying the command was not found.

 

PYTHONPATH Variable

The PYTHONPATH variable is specific to Python. It tells Python where to look for modules and packages that are not part of the standard library. When you import a module in your Python script, Python searches through the directories listed in PYTHONPATH to find the module.

For example, if you have written your own module and want Python to find it, you can add the directory where your module is located to PYTHONPATH. This way, when you use the import statement in your script, Python knows where to look for your custom module.

By understanding and using PYTHONPATH and PATH, you can control where your programs and Python modules are located, making it easier to run and develop your code.

 

This tutorial details how to add the PYTHONPATH and PATH variables on the Raspberry Pi OS image from Raspberry Pi to use with our Python libraries, allowing you to use the libraries and your Python scripts from any directory on your Raspberry Pi.

 

Step 1: Download the Python library from our GitHub Repository and extract the library to the directory of your choice, i.e. ABElectronics_Python_Libraries/ADCPi.

 

Step 2: Use CD to change to the directory containing the library files.

cd ABElectronics_Python_Libraries/ADCPi

 

Step 3: Use the pwd command to obtain the full directory path.

pwd

This will return the full path to the directory: /home/pi/Desktop/ABElectronics_Python_Libraries/ADCPi.

 

Step 3 a:  We now edit the .bashrc file to include the library folder in the PATH and PYTHONPATH variables

sudo nano ~/.bashrc

 

Step 4:  Add the following lines to the .bashrc file at the end, and replace this demo path with the correct location on your computer.

PATH="${PATH}:/home/pi/Desktop/ABElectronics_Python_Libraries/ADCPi"
export PATH

PYTHONPATH="${PYTHONPATH}:/home/pi/Desktop/ABElectronics_Python_Libraries/ADCPi"

export PYTHONPATH

Save your changes and exit the Nano editor.

 

Step 5: Reboot to apply the changes

sudo reboot

 

When your Raspberry Pi has rebooted, you will have PYTHONPATH setup for Python support.

Home

Shop

Learn

Forum

FAQ

Contact

0 item

Your cart is empty

Please browse our shop to order from the wide range of Raspberry Pi boards and accessories.

Subtotal:£0.00
View Basket Continue to Checkout