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.
callfor Python. That's only used to invoke other batch files. Have you doneset PYTHONPATHto see exactly what the variable is? Have you tried a script that just doesprint(sys.path)to see what it is getting?