Reputation: 909
How can I execute python function from a package without main / pip install in editable mode
e.g.
my sample.py is
def cli():
print("I'm running myTest")
return 0
and setup.py as
setup(
name="sample",
version="1.0",
py_modules=["sample"],
include_package_data=True,
entry_points="""
[console_scripts]
sample=sample:cli
""",
)
now if I do pip install -e .
and execute sample
I get desired o/p as
I'm running myTest
However with this I'm not able to debug it, What's ideal way to execute function from a module without pip install / using another python file,
also tried with python -m sample
but that gives no o/p since there is no entrypoint
Upvotes: 0
Views: 188
Reputation: 481
So call your function in your sample.py
:
def cli():
print("I'm running myTest")
return 0
if __name__ == "__main__":
cli()
Then, from the command line:
python sample.py
See here for what if __name__ == "__main__":
means:
https://www.freecodecamp.org/news/if-name-main-python-example/
If you want to leave sample.py
completely unmodified, then you just create a new file test_sample.py
and import sample
to use it like so:
import sample
sample.cli()
or, as an alternative method of import, test_sample.py
can also be:
from sample import cli
cli()
Then you run:
python test_sample.py
Just make sure test_sample.py
is in the same directory as sample.py
.
Upvotes: 2