Python adds environment variables

Adding Python to Environment Variables

Adding Python to Environment Variables

When using Python, we often need to add the Python path to the system’s environment variables. This allows us to conveniently execute Python scripts or programs from anywhere. This article will detail how to add the Python path to the environment variables for more convenient use. Python

Why Add the Python Path to Environment Variables?

On Windows, if we don’t add the Python path to the system environment variables, we can only execute Python scripts or programs from the Python installation directory. If we want to execute Python scripts from other directories, we need to enter the full Python path, which is cumbersome. Adding the Python path to the environment variables, however, allows us to execute Python scripts directly from anywhere, greatly facilitating usage.

How to Add the Python Path to Environment Variables

Windows

On Windows, there are two ways to add the Python path to the environment variables: one through the Control Panel, and the other by directly modifying the system environment variables. We’ll explain each method below.

Through the Control Panel

  1. First, open the Control Panel, search for “Environment Variables” in the search box, and click “Edit System Environment Variables.”
  2. In the window that pops up, click the “Environment Variables” button.
  3. Find the variable named “Path” in the system variables and double-click it, or select it and click “Edit.”
  4. Click “New” in the pop-up window, then enter the Python installation path, for example, C:Python39, and click “OK.”
  5. After confirming the changes, double-check that the Python path has been added to the environment variables.

Directly modify the system environment variables

  1. Right-click “This PC” and select “Properties.”
  2. Click “Advanced System Settings.”
  3. On the “Advanced” tab, click the “Environment Variables” button.
  4. Find the variable named “Path” in the system variables and double-click it.
  5. Add the Python installation path to the end of the variable value, separated by a semicolon, for example, C:Python39.
  6. After confirming the changes, re-confirm that the Python path has been added to the environment variables.

macOS System

On macOS, there are two ways to add the Python path to the environment variables: one is through the Terminal, and the other is by modifying the ~/.bash_profile file. Both methods are described below.

Through the Terminal

  1. Open the Terminal.
  2. Enter the following command to add the Python path to your environment variables:
echo 'export PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

Edit the ~/.bash_profile file

  1. Open Terminal.
  2. Enter the following command to open the .bash_profile file:
nano ~/.bash_profile
  1. Add the following to the end of the file:
export PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:$PATH"
  1. Press Ctrl+X, then press Y to save the changes and press Enter to confirm.
  2. Enter the following command to make the changes take effect:
source ~/.bash_profile

Testing

To verify that Python has been successfully added to the environment variables, we can enter the following command in the command line to view the Python version information:

python --version

If the Python path is successfully added to the environment variables, the Python version information will be displayed, confirming that the Python path has been successfully added to the environment variables.

Conclusion

Through this article, we have learned in detail how to add the Python path to the system environment variables. This allows us to conveniently execute Python scripts or programs from anywhere, making Python more convenient to use.

Leave a Reply

Your email address will not be published. Required fields are marked *