Setting PYTHONPATH on Windows under Anaconda without elevated privileges
Solution 1
In the Control Panel window for modifying user accounts, there should be an option to change the environment variables just for the current user. It's broken on some versions of Windows, but if it works it's the simplest option.
If that doesn't work, the next simplest option is to use setx.exe in a command prompt. It defaults to the current user. For example:
setx.exe PYTHONPATH "C:\Users\username\Documents\MyLib"
You can also use reg.exe to set the variable manually in the registry. But unlike the above options, this doesn't broadcast a WM_SETTINGCHANGE
message to top-level windows. When Explorer sees that message it reloads its environment from the registry. Without it, you'll have to log off and back on again to see the updated environment variable. With that said, here's an example command using reg.exe:
reg.exe add HKCU\Environment /f /v PYTHONPATH /d "C:\Users\username\Documents\MyLib"
I'm not a fan of permanently setting PYTHONPATH
because the same variable gets used by every version of Python. I'd rather create a shortcut to a batch script that configures the environment the way I need it for a specific task. For example:
@echo off
set PYTHONPATH=C:\Users\username\Documents\MyLib
C:\Users\username\Documents\MyEnv\Scripts\activate.bat
Create a shortcut to this batch script. Then right-click the shortcut and select "Properties". Modify the target to run cmd.exe /k "path\to\the\script.bat"
.
Solution 2
Since you said, you mostly use Spyder, the following might be useful:
Spyder offers the option to manage PYTHONPATH through an integrated manager.
Mad Physicist
Programming is fun. See my GitHub profile for, among other things, a list of SE posts that turned into open source contributions.
Updated on April 14, 2020Comments
-
Mad Physicist about 4 years
I have a Windows 7 machine with a 32-bit install of anaconda, installed for the local user. The install works as expected: it allows me to run python and pip from the command line, switch environments, etc.
I also have a library of code that I wrote sitting in My Documents. I would like to add an entry like
C:\Users\username\Documents\MyLib
to myPYTHONPATH
. This would normally not be an issue, but I do not have the elevated privileges that this (work-issued) computer requires to modify environment variables through the Windows UI.I am looking for a way to set PYTHONPATH. I only need it to work within the anaconda environment (i.e., I just want to be able to import my library using that particular interpreter). Solutions using
sys.path.append
within my scripts are not acceptable. I am, however, OK with doingsys.path.append
in some script that will be executed automatically whenever I runpython myscript.py
.I suspect that there is a way to get around the privilege restrictions imposed through the Windows UI though, because running
conda info -a
shows that the Anaconda installer was able to insert items into thePATH
, specifically,C:\Users\username\AppData\Local\Continuum\Anaconda3\Library\bin;...
.