12th Anniversary Sale 12% Off in July & August with the voucher code "SUMMER12" *excludes trade customers
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

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.


Was this article helpful? Yes No

Please tell us how we can improve this article:

Submit

Related Expansion Boards

Order a ADC Differential Pi Photo of ADC Differential Pi
ADC Differential Pi

8 Channel 18-bit Differential Analogue to Digital converter for the Raspberry Pi

£17.99 Ex VAT: £14.99
Order a ADC Pi Photo of ADC Pi
ADC Pi

8 Channel 17-bit Single-Ended Analogue to Digital converter for the Raspberry Pi

£19.19 Ex VAT: £15.99
Order a Expander Pi Photo of Expander Pi
Expander Pi

Analogue, Digital and RTC Expansion Interface for the Raspberry Pi

£27.59 Ex VAT: £22.99
Order a I2C Switch Photo of I2C Switch
I2C Switch

4 channel I2C Switch for the Raspberry Pi

£10.79 Ex VAT: £8.99
Order a IO Pi Plus Photo of IO Pi Plus
IO Pi Plus

32 channel MCP23017 digital I/O expander for the Raspberry Pi

£14.39 Ex VAT: £11.99
Order a IO Zero 32 Photo of IO Zero 32
IO Zero 32

32 channel digital I/O expander for the Raspberry Pi

£13.19 Ex VAT: £10.99
Order a RTC Pi Photo of RTC Pi
RTC Pi

Real-time clock for the Raspberry Pi

£11.39 Ex VAT: £9.49
Order a Servo PWM Pi Photo of Servo PWM Pi
Servo PWM Pi

16-channel, 12-bit PWM controller for driving LEDs and RC servos

£11.39 Ex VAT: £9.49

Related Articles

Raspberry Pi GPIO Pins
Raspberry Pi GPIO Pin mapping and connections
PCB Header Assembly Jig
Using the PCB Header Assembly Jig
Samba Setup on Raspberry Pi
How to install and setup Samba on the Raspberry Pi
Set a static IP Address on Raspberry Pi OS Bookworm
How to use a static network IP Address on the Raspberry Pi
test
test
I2C Part 1 - Introducing I2C
An introduction to the I²C protocol
I2C Part 2 - Enabling I2C on the Raspberry Pi
How to enable I2C on the Raspberry Pi
I2C Part 3 - I2C tools in Linux
An introduction to I²C tools in Linux
I2C Part 4 - Programming I2C with Python
Get started with Programming I²C with Python and SMBus
SPI and Python on Raspberry Pi OS
Enabling SPI on the Raspberry Pi and installing python py-spidev
Using Pythonpath with our Python Libraries
Adding the library path to startup
Connecting Expansion Boards to the Raspberry Pi 400
Expanding the Potential of Your Raspberry Pi 400