0

how can i convert my python file to cmd command that can be run from anywhere on pc

for example let's call it my_file.py

i want the user to type the file name (without needing to specify the extension) and run the file

C:\Users\alsaibi\Desktop>my_file

and then the program execute

here it just print "Hello, World"

Hello, World

i want to upload this file to pip so the user download it easily and it will be easy to use from cmd easily

i saw it being done in some other projects in github

you just use pip

and can use the command in cmd without cd or extension

i'm using windows 10

edit:

project on github that use something like that

youtube-dl work just by typing youtube-dl and arguments

i want my project to work like that

another project on github

the file execute using only the file name

1 command and the file execute and you can download it via pip

  • 5
    Does this answer your question? [Create a single executable from a Python project](https://stackoverflow.com/questions/12059509/create-a-single-executable-from-a-python-project) – ncopiy Jun 06 '20 at 17:48
  • @ncopiy that doesn't answer my question –  Jun 06 '20 at 17:49
  • @ncopiy i want to run it from cmd as command and not turn it into exe –  Jun 06 '20 at 17:50
  • @ncopiy i tried turning it to exe but there are a lot of issues –  Jun 06 '20 at 17:51
  • Are you looking to package and distribute your program? I thought the question that @ncopiy linked would be relevant, no? – AMC Jun 06 '20 at 18:49
  • I don't have Windows handy to check, but you can use `assoc .py=python` and `ftype python=c:\path\to\python.exe` to make a .py file executable. - logout / login to make it work. – tdelaney Jun 06 '20 at 18:55
  • @AMC i want to distribute my program with another method i know how to distribute the program in the method above but that's not what i'm looking for –  Jun 06 '20 at 19:22
  • @alsaibi _i want to distribute my program with another method_ Can you expand on that? _i know how to distribute the program in the method above but that’s not what I’m looking for_ Which method are you referring to, specifically? – AMC Jun 06 '20 at 19:24
  • @tdelaney i don't know what that command does but i tried it and logged out but it didn't work –  Jun 06 '20 at 19:26
  • setuptools setup.py's `entry_points["console_scripts"]` can build .exes. See https://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation. Also, you can goto pypi's [search page](https://pypi.org/search/), filter for Windows and browse projects for examples. – tdelaney Jun 06 '20 at 21:05

3 Answers3

1

You should create a setup.py for you script and define a console_scripts entry point.

Writing the Setup Script

Packaging Command Line Scripts

After installing it with pip it should work as you described.

dominik
  • 179
  • 7
0

You could create a file called my_file.bat and then in it write:

python3 my_file.py

and then just add both the .py and .bat it to the PATH. Then, you can achieve your goal: typing

my_file

and then having the output be

Hello, World
Donoru
  • 115
  • 1
  • 15
-1

Follow and execute these commands in order

  1. Rename your file

    mv my_file.py my_file

  2. Add this to the beginning of your file:

    #!/usr/bin/env python

  3. Make the file executable:

    chmod +x my_file

  4. Now you should be able to run the file as:

    ./my_file

  5. To run the file anywhere on the PC you have to move the file to /usr/local/bin

    mv nameofthescript /usr/local/bin

GOVIND DIXIT
  • 1,748
  • 10
  • 27