The editor of Downcodes brings you a detailed tutorial on setting Python environment variables under Linux systems. This article will explain in detail how to set Python environment variables from four aspects: using the export command for temporary settings, editing the Bash configuration file for persistent settings, modifying the environment file to achieve global settings, and using Python modules for dynamic management. Whether you are a Linux system administrator or a Python developer, I believe you will benefit a lot from this article and quickly master the skills of flexibly using Python environment variables in Linux systems to improve development efficiency.
Setting environment variables in Python under Linux usually involves editing shell configuration files, using the export command, modifying environment files, or using specialized Python modules. Setting environment variables is very important for configuring system behavior, specifying prerequisites for program running, and managing multiple versions of software. The most straightforward way is to use the export command, which temporarily sets environment variables until the current terminal session ends. Permanent configuration involves editing shell configuration files such as .bashrc or .profile. In addition, the management of environment variables can be achieved through Python's os module, where the os.environ dictionary stores environment variables at runtime, allowing scripts to dynamically read and set environment variables.
1. Use the EXPORT command to set temporary environment variables
The Export command is used to set or export environment variables in the current session. By entering export VARIABLE_NAME=value in the terminal, you can create a new environment variable or modify an existing environment variable.
For example, setting the PATH variable can be done like this:
export PATH=$PATH:/usr/local/bin/python3
This operation will append a new path to the existing PATH, and this variable will only take effect in the current session of the current user.
2. Edit the BASH configuration file for long-term settings
If you need environment variables to remain valid across all sessions and reboots, you should edit the user's bash profile file, usually .bashrc, .profile, or .bash_profile, which are located in the user's home directory.
Open the terminal and enter nano ~/.bashrc (use the nano text editor here, you can also use vi or other editors). Add the following statement at the bottom of the file to set the PYTHONPATH environment variable:export PYTHONPATH=/usr/local/lib/python3.8/site-packages
Save and close the file. For the changes to take effect, source ~/.bashrc, or restart a terminal session.3. Modify environment files
For global environment variables that are valid for all users, you can edit /etc/profile (valid for all shells), or /etc/environment (use a specific format, only the key-value pairs of environment variables can be stored, and there can be no export command) . For example, edit /etc/environment:
sudo nano /etc/environment
Then add the following lines to the file:
PYTHONPATH=/usr/local/lib/python3.8/site-packages
Remember that you need to restart the system or log in again after making changes for the changes to take effect.
4. Use PYTHON MODUEL to manage environment variables
Python's os module allows getting and setting environment variables directly from Python scripts. This makes it possible to adjust variables temporarily while different programs and scripts are running.
For example, this can be set in a Python script:
import os
os.environ['PYTHONPATH'] = /usr/local/lib/python3.8/site-packages
print(os.environ['PYTHONPATH'])
Adjusting environment variables by a Python script is only effective in the process in which the script is running and will not affect other parts of the operating system.
Through the operation of environment variables, Python ensures that various conditions and parameters required for program running are met in the Linux environment, improving the flexibility and configurability of the software. Especially in the development and deployment stages, setting environment variables appropriately is an indispensable part, whether for system administrators or ordinary users.
1. How to set Python environment variables in Linux?
First, open a terminal and use a text editor to open the ~/.bashrc file (replace it with any other shell configuration file you use). Second, find a blank line in the file, or add a new line at the end. Then, on a new line, add the following: export PATH=$PATH:/usr/local/python (where /usr/local/python is the installation path of your Python interpreter). Finally, save and exit the editor and run the following command in the terminal for the changes to take effect: source ~/.bashrc.2. How to verify whether Python environment variables are set successfully?
First, open a terminal and enter the which python command. Second, the command line will display the path to the Python executable. If the displayed path is the installation path of the Python interpreter you set, the setting is successful. Then, enter the python --version command to ensure that the Python version displayed is consistent with the version you installed. Finally, you can try running the Python interpreter directly from the command line to make sure it starts properly.3. How to add multiple Python environment variables in Linux?
First, open a terminal and use a text editor to open the ~/.bashrc file (replace it with any other shell configuration file you use). Second, find a blank line in the file or add new content at the end. Then, add multiple Python environment variables in the following format: export PATH=$PATH:/path/to/python1:/path/to/python2 (replace /path/to/python1 and /path/to/python2 with what you want Add the installation path of the Python interpreter). Finally, save and exit the editor and run the source ~/.bashrc command in the terminal for the changes to take effect. Now the system will search for and use any Python interpreters you added.I hope this tutorial provided by the editor of Downcodes can help you better understand and use Python environment variable settings under Linux systems. If you have any questions, please leave a message in the comment area.