How to make a command that runs Python a script in Windows command line?
Question:
Background:
I’m using Windows. I know some of programming with python. I don’t know much about batch
, which 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.
e.g.
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 explanation 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 does what I want to know is the same thing.
Answers:
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_directorypython.exe python_script_directorymyprogram.py %*
Configurations to make:
You need to set in PATH
environment variable the location of the batch file batch_file_directorymyprogram.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.
Background:
I’m using Windows. I know some of programming with python. I don’t know much about batch
, which 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.
e.g.
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 explanation 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 does 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_directorypython.exe python_script_directorymyprogram.py %*
Configurations to make:
You need to set in PATH
environment variable the location of the batch file batch_file_directorymyprogram.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.