DEV Community

PyBash
PyBash

Posted on • Originally published at pybash.hashnode.dev on

Command-Line Utility: Part 2 - How to get rid of the ".py" extension

Introduction

This post is a continuation of the previous post "How to make a Command-Line Utility in Python". If you have not seen it yet, click here.

Prerequisites

Alright, so we are going to be needing a few modules and tools for this. First, we need pyinstaller and second, we need auto-py-to-exe

Explanation

On Windows(not sponsored by Microsoft), if you want to access the file without the ".py" extension, we need to convert it to an executable or ".exe" file. So we are going to be using these two tools to make it an executable. pyinstaller is pretty popular, and I am sure a lot of you already know about it. And how to use it.

Installation

pyinstaller

Official Way

Steps:

  1. Click on Start
  2. Type "Command Prompt" and click Enter on your Keyboard
  3. A new command prompt window should open
  4. Now, type pip install pyinstaller
  5. And done!

Unofficial Way

Warning! - This is not the official pyinstaller wheel. Only use this if for some reason the official way fails. Steps:

  1. Go to ifd.uci.edu/~gohlke/pythonlibs/#pyinstaller
  2. The first link at the top of the page, is to be downloaded. The name of the file is pyinstaller-4.2-py3-none-any.whl. This is only for Python3.
  3. Download the file and save it on the Desktop
  4. Then, click on start
  5. Type "Command Prompt" and click Enter on you keyboard.
  6. A new command prompt window should open
  7. Now type the following commands:
C:\Users\[username]> cd Desktop
C:\Users\[username]\Desktop> pip install pyinstaller-4.2-py3-none-any.whl
A lot of text keeps on scrolling and then shows, successfully installed pyinstaller

Enter fullscreen mode Exit fullscreen mode
  1. And done!

auto-py-to-exe

Steps:

  1. Click on Start
  2. Type "Command Prompt" and click Enter on your Keyboard
  3. A new Command Prompt Window should open
  4. Now, type pip install auto-py-to-exe
  5. And done!

Converting to exe

Steps:

  1. Open windows explorer and navigate to the folder where you have your CLU(Command-Line Utility) file stored.
  2. Now open another Command Prompt window
  3. And, type auto-py-to-exe
  4. A new window should pop-up as given below auto-py-to-exe_window_image
  5. Now, copy the path to the file from the explorer and paste it in the Script Location box. For e.g., - D:\Programming\CLI-Utilities\search.py If the path is correct, the border should turn to red from blue
  6. Then, select One File and Console Based
  7. Then expand Settings
  8. Under output folder, enter the folder path where, the .py file is located For e.g., - D:\Programming\CLI-Utilities Refer to the above image
  9. And then, click on "CONVERT PY TO EXE" button, And a lot of output will be shown.
  10. Then go to the directory where the script is located and an exe named search.exe should be there.
  11. Now, go back to the Command Prompt
  12. Now type, only search and pass the arguments and it will work like a charm!

Conclusion

Maybe, you will be wondering, why can't we just rename the file to search from search.py and it should work, just like in Unix right? No, windows does not work like that! If you did that and typed search, then windows would simply search the PATH variable for an exe named "search", and since it won't find any, it would just say, that Not Found... So we need to convert it to an exe to work as intended.

Top comments (0)