简体   繁体   中英

Turning files of Python code into executables

I am reading O'Reilly Learning Python (4th edition). There is a paragraph called Unix Executable Scripts (#!) (page 46).

The example given is the following:

`Use your text editor again to create a file of Python code called brian:

#!/usr/local/bin/python
print('The Bright Side ' + 'of Life...')

` Like in the example, I save this script in a file named 'brian' (no .py, as it is not required).

I then give the file the privileges chmod +x brian

It is then said that I can 'run it from the operating system shell as though it were a binary program:

% brian
The Bright Side of Life...

'

However, when I try from my command window to call "brian", I get the following error:

bash: brian: command not found

But python brian gives me the correct result.

Why calling 'brian' like in the example doesn't work for me?

You need to do ./brian . Unix will then look for it in the current directory. Your current directory may not be in the system path and hence it is unable to find a command named brian .

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