Reputation: 1277
I created an open source python package to translate text or automate translations using different translators and published it on pypi. It is working great so far and I received some good feedback and criticism about it.
Now I'm not here to promote the project. Instead, I want to ask, how or what is the right way to make the package usable from terminal. Like in my example the project name is deep_translator. My goal is to be able to use it from terminal. Precisely something like this:
$ deep_translator translate --translator "google" --from "german" --to "english" --text="blabla"
I understand that if someone installs my package on his machine, the package will be likely added to the Path. Hence, it can be accessed from terminal but this must be depending on the OS, right?
So what is the right way to do this. I'm working with python>=3.6 but this is actually a general question and not python specific. Thanks in advance
Upvotes: 1
Views: 520
Reputation: 36299
You can register a command line script in your setup.py
file. When the package gets installed this places a binary on the path, which invokes the configured function. For example:
# setup.py
setup(
...
entry_points = {
'console_scripts': ['deep_translator=deep_translator.main_module:main_func'],
}
...
)
Here main_module
is a hypothetical module hosting a function main_func
which will be invoked. In this function you can setup an ArgumentParser
and parse the arguments:
# main_module.py
def main_func():
parser = argparse.ArgumentParser()
parser.add_argument(...)
# more arguments here
args = parser.parse_args()
# do stuff with args
__main__.py
You can also use a top-level __main__.py
module and then invoke your package via the -m
switch:
python -m deep_translator <args-go-here>
Upvotes: 1