-2

I use simple command

import os

os.system("set Var=test") 
print("echo %Var%")
os.system("set /P Var=<test2.txt") 
print("echo %Var%")

but it can't set batch file variable Var? How to set batch variable from python?

Thank you.

yensheng
  • 1,315
  • 2
  • 14
  • 22
  • 1
    Your question requires more clarification; What is the variable for? and how is it to be used? Is it supposed to be local to a particular batch file, or to the python script? or for use after one, or both, of those scripts have completed? Is the variable to be available to a particular end user, or for all machine users? – Compo Apr 14 '22 at 08:26
  • Please read first my long answer on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) The chapter __F)__ is most important for you. __No process can modify the environment variables of an already running process!__ – Mofi Apr 15 '22 at 18:12
  • Then read the Python documentation for [os.system(command)](https://docs.python.org/3/library/os.html#os.system). The usage of this since many years deprecated function results on Windows in the execution of `cmd.exe` with the option `/C` and the command specified in Python script code appended as additional argument(s). There is used the environment variable `ComSpec` which is defined by default on Windows with `%SystemRoot%\System32\cmd.exe` which expands usually to `C:\Windows\System32\cmd.exe` and results in execution of this executable (64-bit) or `C:\Windows\SysWOW64\cmd.exe` (32-bit). – Mofi Apr 15 '22 at 18:25
  • So whatever you want to do which is really unclear, it cannot be done with the code posted by you. It is impossible to run `cmd.exe` to define an environment variable which should be set in memory of `python.exe` processing the Python script file. The environment variable is defined in memory of started `cmd.exe` which closes itself immediately after finishing the execution of `set Var=test` (first started `cmd.exe`) or `set /P Var= – Mofi Apr 15 '22 at 18:25
  • The Python documentation about [os.environ](https://docs.python.org/3/library/os.html#os.environ) describes how to get and set environment variables of the Python process currently executing the Python script containing your Python code. The current list of environment variables of Python process are copied by the Windows kernel library function [CreateProcess](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) on pointer `lpEnvironment` being a null pointer as it is usually the case when an executable starts another executable on Windows. – Mofi Apr 15 '22 at 18:28
  • The Python [subprocess module](https://docs.python.org/3/library/subprocess.html) is on Windows a Python wrapper class for `CreateProcess` and the [STARTUPINFO](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure. `subprocess.run` or `subprocess.call` or `subprocess.Popen`. The last one offers the possibility to define a special list of environment variables passed via function parameter `lpEnvironment` to Windows kernel function `CreateProcess` which creates the environment variables in memory of the process to create/start. – Mofi Apr 15 '22 at 18:34
  • 1
    You wrote that you are a beginner in Python programming. Please forget immediately everything read about `os.system` and never use in future this deprecated function again. If you want to run another executable from within a Python script, there is `subprocess` for this purpose. Please note further that Python is a much more powerful script interpreter than the Windows Command Processor. There is (nearly) never the need to make use of `cmd.exe` from within a Python script as everything done by `cmd.exe` can be done also with native Python code, just take the time for the research. – Mofi Apr 15 '22 at 18:37

1 Answers1

-1

2 ways you could do this.

The first and most easy is to just run the echo command from os:

import os

os.system('set Var="test"\necho %Var%')

This will print your variable to the python command line

or you could write it to a file the run the file:

import os

with open("file.bat", "w") as wFile:
    wFile.write('set Var="test"\necho %Var%')
os.system("file.bat")

ItzTheDodo.

EDITS: Updated examples

ItzTheDodo
  • 100
  • 8
  • I suggest to read [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) to learn the difference between `set Var="test"` and `set "Var=test"`. Did you try `os.system('set Var="test"\necho %Var%')`? I doubt that this really works because of `os.system()` results in starting `%ComSpec% /c set Var="test"LFecho %Var%` with `%ComSpec%` expanding usually to `C:\Windows\System32\cmd.exe` and `LF` being a line-feed. A line-feed (decimal code point value 10) in a command line argument string to execute by `cmd.exe`? – Mofi Apr 15 '22 at 18:00
  • I don't have Python installed and so could not run this line and look what really happens. But let us assume there is used the right syntax, i.e. `os.system('set "Var=test" & echo %Var%')`. That would also not work because of `cmd.exe` first parses the entire command line and then executes the two commands, see [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](https://stackoverflow.com/questions/4094699/) and [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/). – Mofi Apr 15 '22 at 18:07