LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Bodhi
User Name
Password
Bodhi This forum is for the discussion of Bodhi Linux.

Notices


Reply
  Search this Thread
Old 06-17-2019, 10:27 AM   #1
rrashkin
Member
 
Registered: Aug 2018
Location: San Benito, Texas, US
Distribution: Bodhi Linux
Posts: 81

Rep: Reputation: Disabled
making a Python script executable


I'm sorry if this is asked and answered elsewhere but I still can't get the hang of searching this forum usefully.

I made a Python script executable (step 1) by CHMODing 755:

>sudo chmod 755 phonepad.py

I attempted to accommodate CL execution with this line:
#! /usr/bin/env python

I'm guessing the path to "env" is not /usr/bin. In any case it didn't work. What's the magic spell?
 
Old 06-17-2019, 10:47 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
1) Type "which env" to see what directory it is found in (assuming it is) from your PATH.

2) Type "which python" to be sure it is in your PATH.

3) Type "echo $PATH" to see the definition of PATH variable.

4) Your shebang appears to have an extraneous space. Type it without that first space space:
Code:
#!/usr/bin/env python
 
2 members found this post helpful.
Old 06-17-2019, 01:17 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Quote:
In any case it didn't work.
What did not work. Did you get an error? or the script did not execute? Are you sure that the script is any good? You have the correct modules installed?
Will the script run with python myscript.py
Are you executing a python2 script with python3? You left a few things out.
 
1 members found this post helpful.
Old 06-17-2019, 03:36 PM   #4
rrashkin
Member
 
Registered: Aug 2018
Location: San Benito, Texas, US
Distribution: Bodhi Linux
Posts: 81

Original Poster
Rep: Reputation: Disabled
Thanks for contributing.

I'm running python 2.6
"python scriptname.py" works fine

"it didn't work" meant that it did not execute.
 
1 members found this post helpful.
Old 06-18-2019, 07:39 AM   #5
wvermin
Member
 
Registered: Jun 2019
Posts: 34

Rep: Reputation: Disabled
What command did you give to execute the script?

This will probably NOT work

scriptname.py

But this will probably run:

./scriptname.py
 
1 members found this post helpful.
Old 06-18-2019, 07:55 AM   #6
rrashkin
Member
 
Registered: Aug 2018
Location: San Benito, Texas, US
Distribution: Bodhi Linux
Posts: 81

Original Poster
Rep: Reputation: Disabled
@wvermin:
That was it! I never would have come up with that. Thank you.
 
1 members found this post helpful.
Old 06-18-2019, 08:18 AM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by wvermin View Post
This will probably NOT work

scriptname.py

But this will probably run:

./scriptname.py
scriptname.py would work if the directory it is in was added to the PATH variable. The "./" only works if one is sitting in the directory where the script is as that is what "./" is saying to use - the current directory. Specifying the full path to that directory would also work.
e.g. If the file's full directory pathname is /mydir/mysubdir/scriptname.py the following would all work:

Code:
cd /mydir/mysubdir
./scriptname.py
-OR-

Code:
/mydir/mysubdir/scriptname.py
-OR-

Code:
PATH=$PATH:/mydir/mysubdir
scriptname.py
If it is a one off run then doing the first method would be the way to go. If it is something you intend to run all the time the easiest method is to modify the user profile (e.g. .profile, .bashrc, .bash_profile) to include the directory in the PATH being defined.
 
Old 06-18-2019, 08:25 AM   #8
rrashkin
Member
 
Registered: Aug 2018
Location: San Benito, Texas, US
Distribution: Bodhi Linux
Posts: 81

Original Poster
Rep: Reputation: Disabled
Thank you @MensaWater. It's not a frequently used app. I could even have been content to use "python scriptname.py" but I wanted to play around. LLL (long live linux).
 
  


Reply

Tags
python2.6



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Script and Making it Executable Chiff Linux - Newbie 9 05-21-2012 07:14 PM
[SOLVED] making script executable rabiticide Linux - Software 3 09-08-2009 09:41 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
making script executable; getting no such file or directory madwida Slackware 4 10-08-2008 09:19 AM
Running a Java executable class from another executable class LUB997 Programming 22 07-24-2005 04:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Bodhi

All times are GMT -5. The time now is 11:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration