简体   繁体   中英

How to set a environment variable for a python script on windows10?

Now I am running the python file everytime like this:

python C:/my/path/to/python/file.py

I want to replace it with one word command like this

myfile

The same command should work from command_prompt, powershell and powershell_ise and it should work from any directory that I am in.

You can basically create Custom Environment Variables . This link will guide how can you do that.

Link: Create Custom Environment Variables in Windows

The file option is interesting because it means you can also create an environment variable to launch a program. For example, you can point an environment variable to any EXE file on your system. When you invoke the variable, it will launch the program. If you have a custom executable program file stored in some random directory on your PC, this is an easy way to launch it without having to go look for it.

Or if you want to run your python script from anywhere, technically not anywhere but from other directory also then you can create a .bat file. And opening that .bat will automatically run your python script. You can create a .bat by :

  1. Right click create a new file. Name it as script.bat .
  2. Right click on script.bat and open it in Notepad.
  3. Copy/paste this script python C:/my/path/to/python/file.py
  4. Save it and run that script.bat . It will automatically run you file.py from here. You can place script.bat in your preferred place like in desktop .

If you made the python an exe and cd into the directory or add it to your environment variables you could call it by myfile or myfile.exe

Another way would be to make a batch file that contains

python C:/my/path/to/python/file.py

and name it myfile which would allow you to call the batch file and it would type out the rest

Also if you cd into C:/my/path/to/python you could type python file.py instead of the whole path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM