3

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.

4
  • 3
    Those aren't environment variables, they are command line programs that are added to the PATH variable Commented Dec 11, 2018 at 23:32
  • @UnholySheep i found this but it doesn't answer my question, should I edit my question? Commented Dec 11, 2018 at 23:40
  • Yes, please edit it to show what you have tried and explain why it doesn't work for you Commented Dec 11, 2018 at 23:48
  • Know that it's not called environment variables and it's actually a command helped my alot, I'm going to edit the question and post the solution that I just found, thanks Commented Dec 12, 2018 at 0:05

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.