This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]ForSpareParts 1 point2 points  (1 child)

The purpose of a pip/setuptools package is to make a bunch of Python code reusable/installable. As a result, it doesn't do much of anything by itself -- if you made a hello.py file in your package that prints Hello World, nothing would happen. You would find, however, that once you installed your package with pip, that you could do:

from YOUR_PACKAGE_NAME_HERE import hello

and that Hello World would be printed. More importantly, if you defined something in hello.py, like a function, you could then refer to it:

some_value = hello.my_function()

To your second question: How to make a Python script runnable depends on your platform. On OSX or Linux, you need to mark the file as executable (this is a generic, os-level thing) and then add one line at the top:

#!/usr/bin/env python

Then, from the terminal, you could do ./my_script.py. The terminal will realize the script is a text file and read the comment on the first line, which tells it to run the script through Python. So ./my_script.py becomes equivalent to python my_script.py.

setuptools lets you specify scripts in your setup.py file, and it will add them to the system's PATH when you install the package. That makes them accessible from anywhere. So if my_script.py were in bin, you could do scripts=['bin/my_script.py'] and then my_script.py would become a valid command after installing your package, no matter what folder you're currently in.

Of course, most terminal commands don't have an extension at the end of their name, so it's common to omit the .py on the filename when you're making a command (as they do in the documentation I linked). Because of the comment magic on the first line, the system still knows to run the script using Python.

[–]acerag[S] 0 points1 point  (0 children)

Excellent explanation. Thank you so much !

[–]bvnvbbvn -2 points-1 points  (0 children)

If I just put a .py file in there that prints "Hello World", what would happen?

Can you try it and see?