0

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.

1
  • 2
    You don't need call for Python. That's only used to invoke other batch files. Have you done set PYTHONPATH to see exactly what the variable is? Have you tried a script that just does print(sys.path) to see what it is getting? Commented Nov 2, 2023 at 6:06

1 Answer 1

1

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]._pth file in the embedded python interpreter. Edit python311._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._pth and use Windows batch to copy it into the embedded interpreter directory each time. For example:

copy python311._pth "%userprofile%\python311"

This works very well on my side.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.