search

Home  >  Q&A  >  body text

(python) How to press win+R and then enter the file name to run it?

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...

PHP中文网PHP中文网2710 days ago1166

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-06-28 09:28:26

    In fact, it just allows you to configure environment variables. If you have a project called pythonCodes, then you have to set your pythonCodes in System Properties->Environment Variables->path Add the absolute path. In this case, if you write a python script in pythonCodes, such as command.py, you can directly run it through win+R, and then enter command.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

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-28 09:28:26

    What it means is to create a pw.bat file and register the directory where this pw.bat is located into the global %PATH%environment variable.
    pw.batThe content is

    @eeeeee xxxxxx %*
    @pause

    Where eeeeee represents the path of the python.exe executable file. This book uses py.exe, but during actual installation, the default Python executable file for most users is python.exe. If this python.exe is not registered globally (that is to say, it is not in the %PATH%environment variable, and the command line directly runs python and cannot be accessed), then the eeeee part here needs to use python 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 the xxxxx part.

    Then, since the path of pw.bat is registered to the %PATH%environment variable, you can call the script by running pw 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 previous py.exe.

    reply
    0
  • Cancelreply