473,699 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

7 New Member
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 2863
Mariostg
332 Contributor
/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 New Member
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 Recognized Expert Moderator Specialist
@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 New Member
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 Recognized Expert Contributor
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 Contributor
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 New Member
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 Contributor
Current directory = . (dot). Therefore, include . in your $PATH. PATH (wikipedia). But you have been warned.
Jul 27 '12 #9
pascor
7 New Member
Expand|Select|Wrap|Line Numbers
  1. But you have been warned. 
Why do you say this ?
Jul 27 '12 #10

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

Similar topics

10
3479
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 -- just my local modules. This is not the same question that would be answered with py2exe or py2app. I don't need to package the script into a binary along with Python. I expect Python to be installed. I also don't want to use distutils to install...
1
1746
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 python script seems to be designed for 'non database' sites, not a site which is using .asp, and has dynamic pages. We are an ecommerce site. What are other ecommerce sites using to create an xml file? Are they using the python script?
3
1688
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 it. I installed python, run the interpreter and the script , but all it will do is say invalid syntax.
1
2430
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 start device PNG". Now I should say that this python script is calling fucntions in R (a scripting languange used in statistics) using the python module RPy, so this I dunno if this is entirely a Python question, because as far as I can see the...
4
2828
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. I want to be able to debug the Python script by using a debug server, like Winpdb (winpdb.org). I use ActivePython 2.5.2.2, Microsoft Visual Studio 2005, and Winpdb 1.3.8.
4
3090
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 is that when she runs the script the filename is replaced by the current file that is being processed. So instead of seeing a series of files being listed down the page, she only wants to see the file currently being processed. I am not sure...
1
3583
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 know how I can call individual functions within the python script? Thanks in advance...
6
31126
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: #!/usr/bin/python : command not foundect.py: line 3: ./Verification_Project.py: line 4: from: command not found : command not foundect.py: line 5: ./Verification_Project.py: line 6: from: command not found import: unable to open X server `'. import:...
2
4748
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 > /tmp/outfile.log 2>&1 & Or, I can put that command in a shell script and run it by typing: ./script.sh Both work great. However I don't know how to put either the command or the shell script in a python.py script and get it to run from there. Let me...
0
1780
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 classical tutorial. First executable starts correctly under Linux but not under windows. Second it crashes right after due to unknown module that haev to be loaded.
0
8628
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.