I just learned python not long ago, and I was reading "automate the boring stuff with python", but I still didn't understand this part. The relevant passages in the book are shown in the figure.
So what is going to happen?
What if pw.py in the previous picture does not exist on the C drive? For example, it exists in the pythonCodes folder of the D drive? I still don’t understand the second picture...Does that file need to be in the same folder as where python is installed?
Thank you, Taoist priests...
In fact, it just allows you to configure environment variables. If you have a project called
pythonCodes
, then you have to set yourpythonCodes
inSystem Properties
->Environment Variables
->path
Add the absolute path. In this case, if you write a python script inpythonCodes
, such ascommand.py
, you can directly run it throughwin+R
, and then entercommand.py
to run it directly...However, I don’t recommend doing this, because it will develop bad habits and rely heavily on environment variables. Moreover, once the project changes location, the environment variables will have to be changed. Instead of doing this, it is better to directly select a working path. Then just develop on it! Environment variables only need to be set in the python program
What it means is to create a
pw.bat
file and register the directory where thispw.bat
is located into the global%PATH%
environment variable.pw.bat
The content isWhere
eeeeee
represents the path of thepython.exe
executable file. This book usespy.exe
, but during actual installation, the default Python executable file for most users ispython.exe
. If thispython.exe
is not registered globally (that is to say, it is not in the%PATH%
environment variable, and the command line directly runspython
and cannot be accessed), then theeeeee
part here needs to usepython The full path of .exe
.Secondly,
xxxxx
is the path to the.py
file. This method is to run a python file permanently, and only enter the parameters that need to be passed into the python file after Win+R. And if you need to enter the Python path in Win+R, you should omit thexxxxx
part.Then, since the path of
pw.bat
is registered to the%PATH%
environment variable, you can call the script by runningpw python script path
directly from Win+R.It doesn’t matter where pw.py is placed, because for these paths, if the directory has been registered to the
%PATH%
environment variable, you can directly write the file name. If the directory has not been registered to%PATH
Environment variables, you must write the entire path, nothing more, the same is true for the previouspy.exe
.