[英]How to make a command that runs python a script in Windows command line?
I'm using Windows. 我正在使用Windows。 I know some of progamming with python. 我知道一些用python编程的方法。 I don't know much about batch
, wich I think I might need to do what I want. 我对batch
不太了解,但我想我可能需要做我想做的事情。
I will show a example so it becomes more clear what I'm trying to do. 我将显示一个示例,以便更清楚地说明我要做什么。
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. 使用git
,安装它后,可以从计算机的任何位置调用git
命令,可以执行git
命令,例如git init
,这将在当前文件夹中创建一个git文件。
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. 我不确定git的工作原理或使用的语言,但我想做同样的事情,创建自己的命令,以便在“安装”程序后可以在计算机的任何位置执行该命令。
I want to make my own command and when I call it, it executes a python script. 我想创建自己的命令,当我调用它时,它会执行python脚本。
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
. 我安装了程序,它创建了一个名为myprogram
的命令,当我在命令行中键入myprogram
时,就像我键入python myprogram.py
。 And myprogram -someargument
would be the same as python myprogram.py -someargument
. 并且myprogram -someargument
与python myprogram.py -someargument
相同。
I'm searched for How to make a environment variable that runs python script? 我正在搜索如何制作运行python脚本的环境变量? but I never get exactly what I want, is always something like How do i set environment variable using python script? 但是我从来没有得到我想要的东西,总是像如何使用python脚本设置环境变量? . 。
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. 我正在寻找有关如何执行此操作的说明,或者至少是教程/指南的说明。
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. 就像UnholySheep在评论中说的那样,它不是环境变量 ,而是命令,所以我甚至更改了问题,我想知道是同一回事。
First you need a python script (obviously) so I created a file called myprogram.py
that have this simple line: 首先(显然),您需要一个python脚本,所以我创建了一个名为myprogram.py
的文件,该文件具有以下简单的代码行:
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: 在需要制作批处理文件之后,在我的情况下,我使用了一个名为myprogram.cmd
的.cmd
文件,该文件具有:
@ECHO OFF
python_directory\python.exe python_script_directory\myprogram.py %*
You need to set in PATH
environment variable the location of the batch file batch_file_directory\\myprogram.cmd
您需要在PATH
环境变量中设置批处理文件batch_file_directory\\myprogram.cmd
And now if you execute in the command line myprogram
it will print This should be from a command . 现在,如果您在命令行myprogram
执行,它将打印这应该来自命令 。
You can also use .exe
or .bat
files. 您也可以使用.exe
或.bat
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.