1

I have a really weird problem with embedding python. If I don't specify PYTHONPATH, Py_Initialize fails with ImportError: No module named site.

If I set PYTHONPATH in cmd and then run my program, it works!

If I set PYTHONPATH programmatically (_putenv_s / SetEnvironmentVariable) it fails with ImportError again.

I've checked that the value is set with system("echo %PYTHONPATH%");, I've made sure multiple times that it is the correct path. I have no idea why it's failing... any ideas appreciated.

Setup: win10 x64, stackless python 2.7 x86 embedded in a C program.

toster-cx
  • 2,287
  • 1
  • 26
  • 32
  • 1
    I'm going to place my bet on failure to account for file system redirection. Without seeing any code, information on the execution environment, nor even the input to `_putenv_s`, this remains to be a guessing game. You'll need to improve your question to receive better answers. – IInspectable Feb 09 '17 at 17:20

2 Answers2

3

https://bugs.python.org/issue28245

I found that python37._pth could be used to configure the sys.path info.

python37.zip
.
Lib
Lib\site-packages

# Uncomment to run site.main() automatically
#import site
Ben Cheng
  • 769
  • 10
  • 25
0

Turns out I had to set PYTHONPATH before, then load the dll with a delay. The python library I have seems to be non-standard / modified.

toster-cx
  • 2,287
  • 1
  • 26
  • 32
  • I have tried but not work. my version is Python 3.7.0rc1 (v3.7.0rc1:dfad352267, Jun 12 2018, 06:15:26) [MSC v.1914 32 bit (Intel)] on win32. Beside, I print sys.path cannot find the PYTHONPATH content. – Ben Cheng Jun 13 '18 at 04:15