0

Suppose I have a hellp.py python file. I found that I only can type command: python hello.py to run my command line, but how can I just type ./hello so that my system can run my hellp.py file?

user144600
  • 529
  • 2
  • 7
  • 18
  • Don't use a `.py` extension on your entry point -- it's an *executable*, not a library/module. If you're using setuptools (setup.py), you can have *it* do the work of generating an entry point that invokes a function of your choice in a module of your choice if you really do want your Python scripts to all be libraries by nature -- but it's unlikely that that's something you really need or want to do (and the setuptools-generated entrypoint is still itself a Python script, not a shell script; there's no need for a shell script anywhere). – Charles Duffy Feb 19 '17 at 05:51
  • BTW, I'd argue that this is very much an XY problem ("I want to do X, but I'm asking how to do Y") -- what you *want* is just to run your Python program by running `./hello`; if a shell script is the right and appropriate way to do that, then let the person answering the question point that out; there's no need to incorporate such an assumption into the question itself. – Charles Duffy Feb 19 '17 at 05:52

1 Answers1

3

If you want to run your Python script as an executable, put the right shebang at the top of your .py file. For example, hello.py might be defined as:

#!/usr/bin/env python

print('hello, world!')

Then you'll need to ensure the execute permission is set on the file:

$ chmod u+x hello.py

When you run ./hello.py, the interpreter currently on PATH (including one in a virtualenv if one is activated) will run the Python script.

There are other ways to do this as well with tools like PyInstaller, Py2App, Py2Exe, but I think this is what you're looking for.

EDIT: I noticed that the question specified wanting to just type ./hello at the command line. There are a couple of ways to tackle this. The first would be to simply knock off the file extension and have the Python file simply named hello. The second (and the way I'd recommend) would be to create a link to the file. I'd recommend creating a symlink:

$ ln -s hello.py hello

This way, the code is still obviously a Python file, but you can access it conveniently.

Andrew
  • 669
  • 7
  • 16
  • 1
    Re: setting up entry points without a `.py` extension, arguably the best practice (certainly, the thing that will work across platforms Python supports, including Windows) is using the setuptools entrypoint mechanism: http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html -- after a program so packaged has been installed, scripts that call your desired functions from your chosen modules will be placed in the user's PATH. – Charles Duffy Feb 19 '17 at 05:56
  • Yeah, `setuptools` is great. I think for someone just getting started with the language or writing a small script (as I suspect the asker is doing), the overhead of using a `setup.py` and then installing it into a virtualenv or even the system site-packages may be a bit overkill. Setuptools is a great option, and is definitely recommended for anything that needs to be maintained or published to production. – Andrew Feb 19 '17 at 05:59