# How to set python environment variable PYTHONPATH on Mac?

To set the Python environment variable PYTHONPATH on a Mac, you can follow these steps:

Open the Terminal app on your Mac.

Navigate to your home directory by typing cd ~ and pressing Enter.

Open the .bash_profile file in a text editor by typing open -e .bash_profile and pressing Enter.

Create a new file called .bash_profile by typing touch .bash_profile and pressing Enter.

Add a line to set the PYTHONPATH environment variable in the file. For example:

$export PYTHONPATH=/path/to/my/python/modules  This sets the PYTHONPATH environment variable to the path /path/to/my/python/modules. You should replace this with the path to the directory where your Python modules are located. Save the file and exit the text editor. Restart the Terminal app to apply the changes to the PYTHONPATH environment variable. Here are some additional examples to help illustrate how to set the PYTHONPATH environment variable on a Mac: ## Set the PYTHONPATH variable to a single directory: $export PYTHONPATH=/Users/username/python-modules


This sets the PYTHONPATH environment variable to the directory /Users/username/python-modules.

$export PYTHONPATH=/Users/username/python-modules:/Users/username/other-python-modules  This sets the PYTHONPATH environment variable to two directories: /Users/username/python-modules and /Users/username/other-python-modules. The directories are separated by a colon. ## Set the PYTHONPATH variable relative to the current directory: $export PYTHONPATH=./my-python-modules



This sets the PYTHONPATH environment variable to the directory ./my-python-modules, which is a directory called my-python-modules located in the current directory.

$export PYTHONPATH=/Users/username/python-modules/*  This sets the PYTHONPATH environment variable to all subdirectories of /Users/username/python-modules. The * is a wildcard that matches all subdirectories of the specified directory. By setting the PYTHONPATH environment variable, you can ensure that Python can find your modules and packages when they are imported into your scripts. This can make it easier to organize and reuse your code, and can help improve the maintainability and scalability of your Python projects. ## Set the PYTHONPATH variable using a relative path to the user's home directory: $export PYTHONPATH=~/my-python-modules


This sets the PYTHONPATH environment variable to the directory /Users/username/my-python-modules, where username is the name of the current user. The tilde ~ is a shorthand for the user's home directory.

Set the PYTHONPATH variable to include the current directory and all subdirectories:

$export PYTHONPATH=$PYTHONPATH:$(pwd)  This sets the PYTHONPATH environment variable to include the current directory and all subdirectories. The$(pwd) command returns the current working directory, and the \$PYTHONPATH variable is appended to it using the colon : separator. This ensures that any existing directories in PYTHONPATH are preserved.

On the whole, setting the PYTHONPATH environment variable on a Mac can help ensure that Python can find the modules and packages it needs to run your scripts. By using the examples above, you can customise the PYTHONPATH variable to meet your particular needs and preferences, and ensure that your Python code is organised and easy to maintain.