简体   繁体   中英

How to make a command that runs python a script in Windows command line?

BackGround:

I'm using Windows. I know some of progamming with python. I don't know much about batch , wich I think I might need to do what I want.

I will show a example so it becomes more clear what I'm trying to do.

Example:

When using git , after you install it, you can call the git command from anywhere of your computer, you can execute git commands, like git init and this will create a git file in your current folder.

I don't know exactly how git works or what language they use but I want to do the same thing, create my own command that I can execute from anywhere in my computer after I "install" my program.

What I'm trying to do:

I want to make my own command and when I call it, it executes a python script.
eg
I install my program and it creates a command called myprogram and when I type myprogram in the command line, it's like if I typed python myprogram.py . And myprogram -someargument would be the same as python myprogram.py -someargument .

What I tried until now:

I'm searched for How to make a environment variable that runs python script? but I never get exactly what I want, is always something like How do i set environment variable using python script? .

Maybe I'm making the wrong question and the result I want are not showing?

I'm looking for a explanating on how to do this or at least a tutorial/guide.

Edit 1:

As UnholySheep said in the comments, it's not environment variable , its commands, so I changed the question even doe what I want to know is the same thing.

Files you need:

First you need a python script (obviously) so I created a file called myprogram.py that have this simple line:

print("This should be from a command")

After you need to make a batch file, in my case I used a .cmd file called myprogram.cmd that have:

@ECHO OFF
python_directory\python.exe python_script_directory\myprogram.py %*

Configurations to make:

You need to set in PATH environment variable the location of the batch file batch_file_directory\\myprogram.cmd

And now if you execute in the command line myprogram it will print This should be from a command .

You can also use .exe or .bat files.

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