Reputation: 13
If I have a main.py
file in a folder, how can I create a command in my PC that, calling only main from any point in the terminal, makes my main.py
file running? Thanks
Upvotes: 0
Views: 376
Reputation: 903
Setting the path and variables in Windows 10
Press the Windows key+X to access the Power User Task Menu. In the
Power User Task Menu, select the System option. In the About window,
click the Advanced system settings link under Related settings on
the far-right side.
In the System Properties window, click the Advanced tab, then Click the Environment Variables button near the bottom of that tab.
In the Environment Variables window (pictured below),
highlight the Path variable in the System variables section and
click the Edit button.
Add or modify the path lines with the paths you want the computer to access. Each directory path is separated with a semicolon, as shown below.
C:...\main.py
Upvotes: 0
Reputation: 184201
If you are on Linux or Mac OS X, make sure the program has a valid shebang line such as #! /usr/bin/python
that points to the Python executable you want to run the script with, then enter chmod +x main.py
. Finally, rename the script to remove the .py
extension.
You can now invoke it (if you're in the same directory) with ./main
. If you want to be able to invoke regardless of the directory you're in, add the script's directory to your PATH
variable.
Upvotes: 0
Reputation: 887
You should add main.py to your PATH. What happens when you are running, for instance, python
is that your terminal looks up the command python
in PATH and runs the program that it is pointing to. You could see it as a kind of shortcut to the program Python.
By adding your program to your PATH, you can tell the computer that if you type helloworld
in your terminal, the terminal should run /my/path/to/helloworld.py
.
I don't know what OS you are on, so here are links for most common OS on how to add a PATH variable.
Upvotes: 2