Modifications to sys.path only apply for the life of that Python interpreter. If you want to do it permanently you need to modify the PYTHONPATH environment variable:
PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH
Note that PATH is the system path for executables, which is completely separate.
**You can write the above in ~/.bash_profile and the source it using source ~/.bash_profile
Modifications to sys.path only apply for the life of that Python interpreter. If you want to do it permanently you need to modify the PYTHONPATH environment variable:
PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH
Note that PATH is the system path for executables, which is completely separate.
**You can write the above in ~/.bash_profile and the source it using source ~/.bash_profile
On MAC OS you can simply find the location of python/python3 by using the command which python or which python3. (works for Linux too)
And it should give something like:
For python
/usr/local/bin/python
For python3
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
Export the path to your bash_profile
In your terminal type
sudo nano ~/.bash_profile
Enter your password and paste the following lines
PYTHONPATH="/Library/Frameworks/Python.framework/Versions/3.9/bin/python3"
export PYTHONPATH
Press control + x to exit, and press y for saving on being asked to save
Press `enter' to return to terminal window
Source it using the following command in terminal, run
source ~/.bash_profile
Path to python3 should be updated now!!
macos - how to set up environment variables in mac terminal and have python scripts pick the variable up effectively? - Stack Overflow
Python path variable - Apple Community
Python's path on MacOS
How to set up python on a new Mac?
How do I add an environment variable in Pythonpath?
How do I create a Pythonpath variable?
What is Pythonpath used for?
Videos
If you want to access the NUM_SAS_WORKERS environment variable in the Python shell, run this command from your terminal:
NUM_SAS_WORKERS=10 python
And once in the Python shell:
>>> import os
>>> int(os.environ.get('NUM_SAS_WORKERS', 1))
10
If you want to access it in a file, very similar:
NUM_SAS_WORKERS=10 python yourfile.py
If you set up environment variables from the terminal command it gets erased as soon as you close that terminal. To set up and keep them you have to update them in .bash_profile file which resides in the home directory as a hidden file. Follow the below commands to update it.
Open terminal
cd ~
(To take you to the home directory of the mac)
open -a "Visual Studio Code" .bash_profile
( To open .bash_profile file, in the place of "Visual Studio Code" you can use any text editor name)
Now, .bash_profile will be opened and there you can set your environment variables and save it.
Example:
NUM_SAS_WORKERS=10
Now run echo $[NUM_SAS_WORKERS] in terminal to read that
After performing the above steps some times still environment variables not available to read in your python project/file. In such cases, you have to restart or log off and log in to your machine that can fix the issue.
To check whether the environment variable is available to your python code run the below script in your python console. This should print all your environment variables.
import os
print(os.eviron)