pythonpython-3.xwindowscmd

How do i run a python program just by typing the script name on windows 10 cmd line?


How do i run a python program just by typing the script name on windows 10 cmd line? Also without having to change directory. I already added my scripts folder and python folder to the path. tried also tu run assoc py.=PythonScript ftype PythonScript=python.exe %1 %*

Here's the program's content:

#! python3
# mapIt.py  - Launches a map in the browser using an address from the command line or clipboard
import webbrowser, sys, pyperclip
if len(sys.argv) > 1:
    address = ' '.join(sys.argv[1:])
else:
    address = pyperclip.paste()

webbrowser.open('https://www.google.com/maps/place/' + address)

I added a screenshot with all the commands i tried so far.enter image description here


Solution

  • I think what you want is to run the file 'mapIt.py' without invoking the keyword python that is:

    >mapIt.py
    

    instead of

    >python mapIt.py
    

    the way to do that in Linux or macOS is simple enough, you can add

    #!/usr/bin/env python
    

    to the top of the file, rename your file from mapIt.py to mapIt make the script executable:

    chmod +x mapIt
    

    But for windows there is no straightforward solution.

    One way you can do it is convert the file into an exe or

    first add a python.exe association for all '.py' files

    > assoc .py=Python
    

    and then

    > ftype Python="<path of your python.exe>" "%1" %*
    

    replace the text in angular brackets (<>) with the path of your python.exe file.