0

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

6
  • 2
    python mapIt.py seems to have worked. It looks like though that the script just doesn't do anything. Also, please post everything as text instead of images. Commented Feb 12, 2020 at 16:40
  • I think the program might actually be running correctly, but then giving you an error message. Can you post the contents of mapIt.py? Commented Feb 12, 2020 at 16:40
  • @Carcigenicate or it does something but outputs nothing. Commented Feb 12, 2020 at 16:41
  • To avoid changing directories you can use the file path, in your case the command would be python .\scripts\mapIt.py (this one only works when you are running it from the C:\Users\Tommaso directory) or python C:\Users\Tommaso\scripts\mapIt.py (this command will work at directory) Commented Feb 12, 2020 at 16:45
  • it is working but just after i cd to scripts and call it by using python mapIt.py Commented Feb 12, 2020 at 18:30

2 Answers 2

3

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.

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

3 Comments

yes i forgot to include #! python3 at the beginning of the script and now i can make it work from everywhere bu still typing the .py extencion; I am happy with that but would be better to know hot to do it without
I already did assoc py.=PythonScript ftype PythonScript=python.exe %1 %*
There is no way to do it without the .py extension, also if you feel like my answer was what you needed please choose it as the correct answer.
-1

As I got rejected since the last question I got rejected of duplicate?? Here is the script that generally generates a .sh/.bat for cross-platform.

"""Setup nrm to enviroment so running in cmd/terminal 

command nrm it will work."""

import os
import sys
import platform
import subprocess

def install_requirements():
    # Install the required packages from requirements.txt
    try:
        subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt', '--user', '--break-system-packages'])
        print("Successfully installed required packages.")
    except subprocess.CalledProcessError as e:
        print(f"Failed to install packages: {e}")
        sys.exit(1)

def create_batch_file(nrm_path):
    batch_file_content = f'@echo off\npython3 "{nrm_path}" %*\n'
    batch_file_path = os.path.join(os.environ['USERPROFILE'], 'nrm.bat')
    
    with open(batch_file_path, 'w') as f:
        f.write(batch_file_content)
    
    print(f'Batch file created at: {batch_file_path}')
    print('You may need to add the directory to your PATH if it is not already included.')

def create_shell_script(nrm_path):
    shell_script_content = f'#!/bin/bash\npython3 "{nrm_path}" "$@"\n'
    shell_script_path = '/usr/local/bin/nrm'
    
    with open(shell_script_path, 'w') as f:
        f.write(shell_script_content)
    
    os.chmod(shell_script_path, 0o755)  # Make it executable
    print(f'Shell script created at: {shell_script_path}')
    print('You can run it by typing `nrm` in your terminal.')

def main():
    # Path to the nrm.py file (my case)
    nrm_path = os.path.abspath("nrm.py")
    
    if not os.path.isfile(nrm_path):
        print("The nrm.py file does not exist in the current directory.")
        sys.exit(1)

    # Install requirements
    install_requirements()

    # Detect the operating system
    os_type = platform.system()

    if os_type == 'Windows':
        create_batch_file(nrm_path)
    elif os_type == 'Linux' or os_type == 'Darwin':  # Darwin is for macOS
        create_shell_script(nrm_path)
    else:
        print("Unsupported operating system. This script only supports Windows, macOS, and Linux.")

if __name__ == "__main__":
    main()

1 Comment

This answer looks not relevant to the question at all

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.