I am using Windows batch as the entrance for users to run a Python script for some simple automation requests. I have referred to the answer. Assuming I have used another batch to copy the embedded Python to %userprofile%. Code as below:
pushd "\\Share\Share Drive\share place"
set oldir=%cd%
cd "%userprofile%\python311"
set PYTHONPATH="%userprofile%\python311";"%userprofile%\python311\Lib";"%oldir%"
call python "%oldir%\scripts_prd\%script_name%.py" %1 "%ori%"
However, this set PYTHONPATH=... doesn't work here. The script can be access but any other modules in the %oldir% cannot be accessed, reporting error 'Module not found'.
Just want to know if the PYTHONPATH should be set by different way for embedded Python interpreter?
Update 1:
Thanks to Tim, I've deleted the call.
I've written a script including print(sys.path) and it returns the below values (userprofile replaced):
%userprofile%\python311\python311.zip;
%userprofile%\python311;
%userprofile%\AppData\Roaming\Python\Python311\site-packages;
%userprofile%\python311\Lib\site-packages;
%userprofile%\python311\Lib\site-packages\win32;
%userprofile%\python311\Lib\site-packages\win32\lib;
%userprofile%\python311\Lib\site-packages\Pythonwin
Apparently set PYTHONPATH doesn't work in this embedded python interpreter.
I also showed os.getcwd and it shows the network folder where the python scripts are stored, and a workaround sys.path.append(os.getcwd()) can be used as workaround. But I believe there must be some better solution.
Update 2:
Thanks to Mofi. I've change the code into
pushd "\\Share\Share Drive\share place"
set "oldir=%cd%"
...
set PYTHONPATH="%userprofile%\python311;%userprofile%\python311\Lib;%oldir%"
which works perfectly in the standard (installed) python interpreter. But it still doesn't work in embedded interpreter.
But thanks to all, I have find the solution and will update very soon.
Thanks to Tims and Mofi, I have updated the batch code but found it is still not useful for the embedded Python. Luckily, I've found the reason and solution.
Conclusion: PYTHONPATH is not supported by embedded Python interpreter. This can be proved from the issue28245 on the python community. It is just impossible to use environment variable in an embedded Python interpreter.
Solution:
python[version]._pthfile in the embedded python interpreter. Editpython311._pth(this is for my interpreter) to hard code the directory. This works for either a local place or a share folder. Pls refer to the document.Technically, I can hard-code the
python311._pthand use Windows batch to copy it into the embedded interpreter directory each time. For example:This works very well on my side.