Ivan Raul Sanchez Diaz
Ivan Raul Sanchez Diaz

Reputation: 351

Windows - how can I run a Python file in any cmd directory?

I have a Python script focused on automating the creation of a repository in github, to execute it I write

py repo.py --name reponame

This works in cmd.exe but only in the directory where the file is, I want it to work anywhere, the tutorial on which I base it does it in linux, I have investigated but I have not found anything

Upvotes: 1

Views: 3592

Answers (2)

Titoot
Titoot

Reputation: 120

well first you have to turn it into an executable by using pyinstaller

pip install pyinstaller

open the main directory

pyinstaller --onefile your-script.py

then copy the executable from the dist folder into a folder added into the PATH

example:

C:\Windows

or add the custom folder into the PATH from Control Panel\System and Security\System and then Advanced system settings , Environment Variables and then system variables and then edit the PATH with your path

and then you can execute it from any directory

Upvotes: 1

rmm
rmm

Reputation: 11

There are two ways to do this in Windows. You can type the full path to the python executable, as in

c:/pythonX/python c:/Scripts/xx.py

Or

You’ll have to make sure Python is added to Path which is a list of system directories that Windows will look in automatically if you try to run an executable file directly from the command line.

To do this open up the Control Panel and navigate to System| Advanced System Settings. Under the Advanced tab click on Environment Variables. Then highlight Path in the system variables area, then click Edit. Click New, and add C:\PythonX\ to the list. Click OK and close. That should allow you to run Python scripts from cmd without specifying the full path.

Upvotes: 1

Related Questions