Setting PYTHONPATH environment variable in Visual Studio C++
Question: I have a C++ program which imports a Python module, along the lines of this
snippet:

    
    
    #include <Python.h>
    
    char python_module[] = "my_module";
    
    Py_Initialize();
    PyObject* pName;
    pName = PyString_FromString(python_module);
    pModule = PyImport_Import(pName);
    

However the module needs to be on the `PYTHONPATH` for this line to actually
load the module. On Mac or Linux this is relatively straightforward - set
`PYTHONPATH` on the term you are running the compiled program from. Is there a
way to do this for Visual Studio C++? Setting the `PYTHONPATH` windows
environment variable hasn't helped.


Answer: Here is scheme to setup module search path:

  1. The script location; the current directory without script.
  2. The PYTHONPATH variable, if set.
  3. For Win32 platforms (NT/95), paths specified in the Registry.
  4. Default directories lib, lib/win, lib/test, lib/tkinter; these are searched relative to the environment variable PYTHONHOME, if set, or relative to the executable and its ancestors, if a landmark file (Lib/string.py) is found, or the current directory (not useful).
  5. The directory containing the executable.

You probably need to restart IDE to get it working.

