473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Run a python script and the #!/usr/bin/python line

7
I'm coming from the MSW development world and a few things in linux/ubuntu for "getting things done" have got me stumped.

If I have my python app in "/usr/local/pyscripts" named "myapp.py", how can I set up things so that I can run it from anywhere on the command line by simply giving the command "myapp arg1 arg2 arg3" ? (I can write bash scripts and set aliases if necessary.)

What (good) is the shebang line for in Python scripts ? What part of the OS uses it ?

TIA :-)
Jul 7 '12 #1
15 2850
Mariostg
332 100+
/usr/local/pyscripts must be in your path which you can set in your .bashrc file.
The shebang thells which python your script will be executed with. So you could have python using python 3 and another one using python 2.7. Very common on Arch Linux, probably others too.
Jul 13 '12 #2
pascor
7
As it turns out, the shebang is somewhat useful only when running during development. Considering its 2 requirements it is far more direct to simply use the command line like :
$ python myapp.py

The shebang line is useless for trying to run myapp.py giving only :
$ myapp

Using the utility cxfreeze is a way [ the only known way ?] to be able to create any kind of executable and be able to place it in an existing dir on PATH such as /usr/local/bin . Once the file [myapp ] is marked as executable I can then run the command :

$ myapp arg1 arg2 ...

from anywhere with no dependencies or further setup.


Thanks
Jul 13 '12 #3
numberwhun
3,509 Expert Mod 2GB
@pascor: I think there is some confusion. The shebang line refers to the location of the Python interpreter. It has no bearing on where your script is located, or whether or not you have to use the full path to execute the script. Its simply telling the script immediately, where it can find python. That way you don't need to use the "python <script name>" method of execution. If you move the script to a system that has python installed in a different location, then you would simply need to modify the shebang to point to that location.

That limitation is able to be circumvented by using the following shebang line in your scripts:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2.  
That way, no matter what *nix system you put the script on, it will invoke the env command with the python option, finding the interpreter immediately. This may also solve your issue of the shebang line not working after you create your executable, but not sure it will solve it for a Windows platform.

Now, as for your ability to execute the script without the absolute path, @Mariostg was correct about modifying your PATH variable to include the directory in your search PATH.

Regards,

Jeff
Jul 27 '12 #4
pascor
7
Thanks - I got it working now. I haven't yet found all the conditions necessary to get shebang lines to work documented all in one place.

I find it very unfortunate that to execute a local script (python, bash, etc.) the prefix "./" must be added to the script filename. This seems pointless and counter-productive as well as being the exact opposite that all Window OSs, python, C compilers, etc., check to locate files.

All in all, I find it easier to create a true executable using cx-freeze and then put it in /usr/local/bin, which is already in PATH. Despite these files being 1MB+ in size, these apps can be run from any working directory and solve my desire to have to give only the command "myapp arg1, arg2 ...". Hard drive space is very inexpensive, unlike during the not-so-distant past.
Jul 27 '12 #5
dwblas
626 Expert 512MB
I find it very unfortunate that to execute a local script (python, bash, etc.) the prefix "./" must be added to the script filename.
As Mariostg already stated, the only paths that are searched are the ones in the .bashrc file's PATH. If your program is in a directory that is searched, i.e. in the PATH, then you can just enter the file name. On Linux you can choose what you want the system to do instead of having all of the decisions made for you.
Jul 27 '12 #6
Mariostg
332 100+
I find it very unfortunate that to execute a local script (python, bash, etc.) the prefix "./" must be added to the script filename. This seems pointless and counter-productive as well as being the exact opposite that all Window OSs, python, C compilers, etc., check to locate files.
I suggest you do a little bit of searching on the topic. It is a security issue. You can modify your $PATH to include the current directory if you want. If you do it, don't do it for root.
Jul 27 '12 #7
pascor
7
Adding every development directory to the PATH is just not feasible or even reasonable. I create them/use them/delete them much too often.

Is there a way to automatically add the CWD to PATH without creating duplicate entries ? In other words, how can I get the OS check the CWD *first* to search for files ?
Jul 27 '12 #8
Mariostg
332 100+
Current directory = . (dot). Therefore, include . in your $PATH. PATH (wikipedia). But you have been warned.
Jul 27 '12 #9
pascor
7
Expand|Select|Wrap|Line Numbers
  1. But you have been warned. 
Why do you say this ?
Jul 27 '12 #10
numberwhun
3,509 Expert Mod 2GB
If you would like to know the reasoning behind why dot is a bad thing to have in the PATH variable, then read this.

As per the article, if you do decide to add it to your path, only add it at the end and not the beginning of the PATH variable. You could simply put this in your .bashrc and open another window:

Expand|Select|Wrap|Line Numbers
  1. PATH = $PATH:.
  2.  
But know that myself and probably most other unix people here are just fine with putting the ./ before our scripts. We don't find it to be an issue because:

#1. We know the security risks behind it and
#2. Most importantly, this isn't a Windows system. Windows is insecure (IMHO) because of its lax nature of how it does things. You aren't going to make this Windows and are just going to have to deal with the nuances of it. They are there for a reason.

Regards,

Jeff
Jul 31 '12 #11
pascor
7
Security (and the lack there of) and the OS getting programs in PATH mixed up generally have nothing to do with each other.

This forum isn't the place to flame Windows security issues. There are plenty of other places to do that.
Jul 31 '12 #12
numberwhun
3,509 Expert Mod 2GB
Believe me, I wasn't flaming Windows, I was pointing out what, in my opinion, is a fact. You made the comparison to Windows in post #5 above, I just added an observation to it.

Further to that, we definitely need to get the point across that searching the current directory first is extremely insecure and should not be done. Personally I would stick to using the ./ in front o things, and always do, versus adding the current directory to my path. Don't learn that lesson the hard way, it can wreak havoc.
Aug 1 '12 #13
Mariostg
332 100+
Really pascor how much time did you spent researching about your problem? Did you even bother looking at the links that were provided to you? I sincerely believe all replies provided to your queries are fair. There are several exemples on the internet about the nasty things that could happen if you set your current directory in the path. As @Numberwhun
said, if you want it in your path, put it last. It is a wise advise.
Aug 1 '12 #14
pascor
7
The security issues amount to just 2 potential problems:

1 - If a user is stupid enough to name a program the same as an already existing program or script name. This implies the user is only slightly smarter than a head of cabbage and doesn't first use the [ which ] utility to avoid name duplications.

2 - If the system is hacked. This implies that the OS is inherently unsafe, anyway, and much more anti-malware protection is needed.

Since I'm not a professional developer, I think the risks are minimal.
Aug 1 '12 #15
numberwhun
3,509 Expert Mod 2GB
They would not have the security warnings against something if it wasn't a concern. You would think that people wouldn't do it, but it happens. Heck, people alias things all the time and get themselves in trouble without even knowing it. Users are the inherent threat that all sys admins guard against, its just the way things go. Doesn't matter the OS, the user is the reason for rules most of the time.

I am not going to go any further with this. You have been given the articles to read on the security risks and the system you are working on is yours. If you take the risk then you have nobody to blame but yourself if something goes wrong. We can only give you advice.

Regards,

Jeff
Aug 1 '12 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Noah | last post by:
I would like to package my main script and all the modules it imports into a single script that will run just as the collection would. It should not need to package standard Python lib modules --...
1
by: michael.buonomo | last post by:
We have been using the Google recommended python script for about a year. We recently realized that the script was not crawling our sites url's, but just our folders which reside on the server. The...
3
by: CarlP | last post by:
How do I run a Python script. I have one that gmail loader needs to run on my email box. Here's the script http://www.marklyon.org/gmail/cleanmbox.py I can't seem to find what I need to run...
1
by: sophie_newbie | last post by:
Hi, I'm running a python script which if I run from the command line as root runs fine. But if I run it through the web-browser as a cgi script gives the following error "Error in X11: unable to...
4
by: Chris8Boyd | last post by:
I am embedding Python in a MSVC++ (2005) application. The application creates some environment and then launches a Python script that will call some functions exported from the MSVC++ application....
4
by: Chris Seymour | last post by:
Hi All, I am working on a python script for my colleague that will walk a directory and search in the different files for a specific string. These pieces I am able to do. What my colleague wants...
1
by: hofsoc20 | last post by:
Hi, I am trying to call a python script (Python 2.5) and implement one of its functions. I have successfully called the script and executed it with jep.runscript("sample.py"). Does anyone...
6
by: mott3510 | last post by:
I wrote a python script and I have copied my file (verification.py) into my http directory. However, when I try to run the script I get many errors... .. : No such file or directoryline 1:...
2
by: gintrado | last post by:
I am very new to Python I'm trying to run a python script from within another python script. I can run the script from a unix command line by typing: nohup python script.py password >...
0
by: Stef le Breton | last post by:
Hi, I need to compile a pyhton tool into an executable as the destination plateform (Solaris, Linux) are not deployed with Python or not all needed modules. I found pyinstaller and follow the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.