简体   繁体   中英

How do I call a python script on the command line like I would call a common shell command, such as cp?

Suppose I've got a script I can run with:

python hello_world.py
>>> "Hello, world!"

How can I configure hello_world.py to be executable without 'python' or './' :

hello_world
>>> "Hello, word!"

EDIT: Thanks all for the suggestions! The shebang and path solution and the python package solution both worked. I checked the python package solution because I liked the added features of tacking the script onto my path via pip install -e . without hand-editing my path variable and catching the script during pip freeze > requirement.txt calls.

If you're on Linux or Unix, at the top your file, it's typically something like

#!/bin/python

or

#!/usr/bin/python

You'll need execution perms to run the file as well, in that manner. Use chmod

chmod +x hello_world.py

If you're not running as root, you may require sudo, and that would be

sudo chmod +x hello_world.py

And then attempt to run with

./hello_world.py 

if you must dispense with the ./:

alias ./hello_world.py=hello_world 

One workaround would be to use aliases, ie alias hello_world='python hello_world.py'

This will of course only work in the folder where hello_world.py lives, but alias hello_world='python full/path/to/hello_world.py' would work anywhere.

This will only work for your bash session, but adding the alias command to your ~/.bashrc will make it permanent.

You can do the following:

  • Make the script executable (using chmod +x hello_world.py)
  • Rename it to hello_world
  • Move it to a directory that is in your PATH (eg /usr/bin)
  1. If the ~/bin directory is not there, create it and move the python script to this directory.
  2. Rename hello_world.py to just hello_world
  3. At the beginning of the script, add

      #!/usr/bin/env python 
  4. then

      chmod +x hello_world 

Note: If you are creating the ~/bin directory in Ubuntu , logout and login again (to run the ~/ .profile file). The default ~/.profile will automatically add the ~/bin directory to your path.

It only needs to be readable for "python hello_world.py" to work. You can chmod +x hello_world.py to make it executable. Append '.' to your PATH environment variable if you need the "./"

Another way would be install the script using setup.py and configure entrypoints like this.

entry_points={'console_scripts': ['hello_world = hello_world:main']},

I just did this for the first time a few hours ago. What you can do is turn your python script into a 'package', then install that package.

I followed this tutorial and it worked for me. If you follow it you will be able to type 'runner' on the cmd line and your script will run.

You could create a python script in your PATH (or add it to your own PATH).

I'm assuming you don't have a $HOME/bin , if you do skip this -

mkdir $HOME/bin

next, create $HOME/bin/hello_world

#!/usr/bin/env python

print "Hello, World!"

then,

chmod +x $HOME/bin/hello_world
export PATH=$PATH:$HOME/bin

Then you can,

$ hello_world
Hello, World!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM