Is it possible to modify PYTHONPATH at runtime?

set pythonpath
pythonpath example
py_setpythonhome example
sys append pythonpath
add to pythonpath

I have a C++ application dynamically linked to the Python interpreter. I want to be able to import python modules from a particular directory. I want to modify the PYTHONPATH for my process so that sys.path will include the paths that I added to the PYTHONPATH. That seems to be the way it works according to this documentation:

However, when I print sys.path from Python-land it has the original contents of PYTHONPATH and not the one I set. Here's an example of what I'm doing (using Boost.Python):

int main(int argc, char* argv[])
  _putenv_s("PYTHONPATH", "C:\\source\\\\modules");
  object main = import("__main__");
  object global = (main.attr("__dict__"));
  exec("import sys\nprint sys.path"), global, global);

PS - I know there are other ways to accomplish my goal, but that's not what I'm asking about. I am wondering why Py_Initialize() doesn't use the current value of PYTHONPATH when setting up sys.path. Or perhaps I've misunderstood how it is supposed to work?

I found cross-platform solution. Before invoke any other python code just execute following python lines:

import sys

c++ - Is it possible to modify PYTHONPATH at runtime?, I found cross-platform solution. Before invoke any other python code just execute following python lines: import sys sys.path.append("C:\\source\\\\modules"). I recently updated my python installation to 2.7 (previously 2.5), and I've noticed a strange problem where I cannot import certain modules that I created. I had no problem before. Normally, I ed

This happens if you're using more than one C runtime library at a time. In this case, your application and the Python DLL are probably linked against different CRTs. Each CRT has its own set of environment variables; changes to the environment made with putenv from one CRT are not visible from getenv calls made with a different CRT.

See the "readEnv" example at

You can fix this by making sure to use only a single CRT, but that's tricky in practice. Debug builds of programs typically use debug CRTs (which enable things like heap checks and API assertions); production DLLs, even when used in debugging, typically use MSVCRT, the production, threadsafe version. I've worked around this by disabling the debug CRTs entirely, setting all builds to "multithreaded dynamic", since maintaining separate debug DLLs is too much of a hassle. You lose some debugging capabilities by doing that.

Python import, sys.path, and PYTHONPATH Tutorial, There are a few different ways you can import a package or a module. You can You are allowed to modify sys.path during run-time. Just be  Editing PYTHONPATH. Python won't just search your computer for the file you're trying to import. You have to tell it explicitly each time where to get it. The PYTHONPATH is a list of directories for your computer to check whenever you type import MyModule into the interpreter.

Check out:

void PySys_SetPath(char *path) Set sys.path to a list object of paths found in path which should be a list of paths separated with the platform’s search path delimiter (: on Unix, ; on Windows).


Py_SetProgramName(argv[0]); That adds dirname(argv[0]) to your PYTHONPATH for you.

Altering PYTHONPATH in a ros node at runtime, This is probably because the PYTHONPATH is not properly set. Altering PYTHONPATH in a ros node at runtime communities (almost) of users that successfully have been able to use current ROS releases with Python 3. At runtime, you can change it with: import sys sys.path.append('') In My Computer, right-click Properties (or press Win-Break), System tab, Environment Variables, System. You can add it if it's not already there. Finally, in the CMD prompt: set PYTHONPATH C:\Python25\Lib;C:\MyPythonLib Or in bash:

As other people have said, you may be running into a CRT mismatch. I was able to get this to work with Python 2.6 and Visual C++ 2008:

#include "stdafx.h"
#include "Python.h"

int _tmain(int argc, _TCHAR* argv[])
  _putenv_s("PYTHONPATH", "C:\\source\\\\modules");
  PyRun_SimpleString("import sys\nprint sys.path");
  return 0;

This output:

['C:\\Python26\\lib\\site-packages\\distribute-0.6.13-py2.6.egg', 'C:\\Python26\
\lib\\site-packages\\virtualenv-1.4.9-py2.6.egg', 'C:\\source\\modules', ...

Another option may be to change to that directory, since the current directory typically ends up on the path, e.g.:


which gives me:

['C:\\Python26\\lib\\site-packages\\distribute-0.6.13-py2.6.egg', 'C:\\Python26\
\lib\\site-packages\\virtualenv-1.4.9-py2.6.egg', 'C:\\Windows\\system32\\python', 'C:\\Python26\\Lib', 'C:\\Python26\\DLLs', 'C:\\Python26\\Lib\\lib-tk',
 'c:\\', ...

1. Command line and environment, When invoking Python, you may specify any of these options: introducing additional runtime checks which are too expensive to be enabled by default. In addition to normal directories, individual PYTHONPATH entries may refer to zipfiles  Modify the PYTHONPATH environment variable to contain the directory where is located before starting the interpreter. Or put in one of the directories already contained in the PYTHONPATH variable. Put in one of the installation-dependent directories, which you may or may not have write-access to, depending on the OS.

It is possible that the Python DLL gets its own copy of the environment when it is loaded. Try loading it with LoadLibrary and GetProcAddress after you've changed the environment and see if that changes anything.

Appending to Your Python Path, sys.path.append("/home/me/mypy"). Greg Ward's Installing Python Modules document has more information on other ways of modifying the  The thread B can modify function code, override builtin functions, modify local variables, etc. Python Imports and Python Modules ¶ The Python import path sys.path is initialized by multiple environment variables (ex: PYTHONPATH and PYTHONHOME ), modified by the site module and can be modified anytime at runtime (by modifying sys.path directly).

How do I set PYTHONPATH and other environment variables for , Setting the PYTHONPATH environment variable is an easy way to make Python installation, but this should be avoided whenever possible.). Since v2.7.5.1 and v3.3.2.1, it is possible to override any environment variable in WinPython at runtime using the new WinPython configuration file named winpython.ini and located in settings WinPython folder (or in your user profile directory if you have removed the settings folder).

Tutorial: Setting Python Path, Python uses a search path referred to as the Python Path to find modules that are starting Python, or by modifying sys.path at runtime before importing modules. file is set as the Main Entry Point in Project Properties then Wing may be able  Is it possible to modify PYTHONPATH at runtime? Randomness in Jython; Does python optimize modules when they are imported multiple times? AttributeError: ‘Context’ object has no attribute ‘wrap_socket’ A plethora of Python OSC modules – which one to use?

Dynamically Changing the Python Search Path, We just conditionally add a directory to Python's sys.path , carefully checking to avoid duplication: def AddSysPath(new_path): """ AddSysPath(new_path): adds  Modifying PATH by adding the directory containing python.exe to the start is a common way to ensure the correct version of Python is launched. To permanently modify the default environment variables, click Start and search for ‘edit environment variables’, or open System properties, Advanced system settings and click the Environment

  • Looks like it should work. (Though you probably want 2, not 4, backslashes between source and modules.) What's it printing for the value of sys.path?
  • ['C:\\source\\test','C:\\windows\\system32\\', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] Note that my system PYTHONPATH is set to C:\source\test and my code changes it to C:\source\modules. So, sys.path is getting the original value.
  • Have you checked that _putenv_s is succesful i.e. returns 0?
  • Strange. I can't see how the embedded python interpreter could see environment variables from before you changed them in this process, assuming you did successfully change them. 2 unlikely possibilities: putenv is failing, or putenv affects only the CRT copy of the environment and there's a separate Win32 copy of the environment maintained by SetEnvironmentVariable, which the python interpreter is using. says putenv should change both, but try SetEnvironmentVariable and see if it helps?
  • Since you are embedding Python you should be able to adjust sys.path directly and not use the environment variable method at all.
  • This worked perfectly for me! In boost::python all it takes is import("sys").attr("path").attr("append")("path/to/files")
  • You can do that from C++ like this I believe: PyRun_SimpleString("import sys; sys.path.append('/some/path')\n"); Only advantage is that it keeps the magic out of the embedded python and in the C++ where the embedding is being done (Also ease of path manipulation in python).
  • @dementedhedgehog - well, I've mention that way is cross-platform that is mean (at least for me) manipulation with path on C++ is more difficult than on Python
  • @dewfy I agree with you python path manipulation is much easier than c++ - I apologize if I didn't make myself clear. As a note I solved this problem for myself using Py_SetProgramName(argv[0]); That adds dirname(prog) to you PYTHONPATH for you.
  • Yeah, as I suspected in a previous comment, this is exactly what was happening.
  • This is a 13:37 answer.