1

I'd like to convert my python script to a module that can be called from the commandline. Kind of like pytest or something. For instance, my script is named egscript.py. I have to run the script like this, >python3 egscript.py [commandline args]

What I want instead is to be able to do it like this, >egscript [commandline args]

How can I achieve this?

Madara
  • 328
  • 1
  • 5
  • 19

2 Answers2

1

You could set an alias in your .bashrc or .zshrc file

alias egscript="python3 /path/to/egscript.py"

Or you could turn it into an executive using something like pyinstaller, and then you can run the script like ./egscript

gzcz
  • 496
  • 3
  • 7
1

As you can see in this link, you can use:

C:\> assoc .py=Python
C:\> ftype Python="C:\python27\python.exe %1 %*"

I suggest you to see the link I put before. If you want to run it as a package like pip install egscript and then use egscript, you have to create a package to upload it into the Python Package Index(PyPy). To make that posible, follow the steps in these links: Build Your First pip Package, Packaging Python Projects.

MrNobody33
  • 6,413
  • 7
  • 19
  • I want to do something similar to [this](https://pypi.org/project/pystress/). So that I can distribute it to other systems as well. – Madara Jun 12 '20 at 08:11
  • 1
    Okay, you mean, run the file, only by his name, without .py, yeah? – MrNobody33 Jun 12 '20 at 08:56
  • As an application like in the link. So I can also pass the commandline arguments and others can install it via pip – Madara Jun 12 '20 at 09:26
  • 1
    Oh, okay. You have to create a proyect with your script and then publish it if you want. You have to follow the steps of this [link](https://packaging.python.org/tutorials/packaging-projects/). – MrNobody33 Jun 12 '20 at 09:52
  • You can see this [link1](https://dzone.com/articles/executable-package-pip-install) too. I find it very helpful. – MrNobody33 Jun 12 '20 at 09:53
  • 1
    Glad it helps you, I just added the links to my answer. I'll appreciate your upvote. :) – MrNobody33 Jun 13 '20 at 06:16