DEV Community

Cover image for Use custom python scripts from any directory in windows
Joshua Hassan
Joshua Hassan

Posted on

Use custom python scripts from any directory in windows

In this one I am going to be showing you how you can run your python scripts from the terminal in directory on windows.

But first we shall look at what a PATH variable means and what ENVIRONMENTAL variables mean

Environmental Variable
An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.

PATH Variable
The PATH variable is an environment variable that contains an ordered list of paths that Linux will search for executables when running a command. Using these paths means that we do not have to specify an absolute path when running a command.

Now that we know what these terms mean we can get into the actual work
First step is to write our custom script, in this case I am using a script I developed to kill annoying tasks because task manager sometimes does not respond
Code snippet



import os
import sys

if len(sys.argv) < 2:
    print("[!] Usage: kill.py 'program name'")
    sys.exit()

program = sys.argv[1]
print(f"[*] Killing {program}")
os.system(f'taskkill -f -im {program}.exe')
sys.exit()


Enter fullscreen mode Exit fullscreen mode

Next step is to create a folder for our custom scripts and add the folder path to environmental variables
create folder
After copying folder path click windows button and
next step
Next step click on environmental variables
click on env varibles
click on path and then edit at the bottom
click on path and edit
click on new and then paste in the path to our custom folder
paste path
And voila we are ready to go. So to test this we open a new terminal and type out the name of our script in this case kill.py you should see the error output
Anyways we go ahead and test this
code results

Use this to automate your boring tasks, Happy Coding Everyone :)

Top comments (0)