Launching a python file


12 posts
by MrSlinkyfish » Fri May 25, 2012 7:20 pm
I am completely new to Linux. I have a python program saved as name.py in the pi folder in debian and I want to run it. ie. launch it from terminal, I have tried typing:

home/pi/name.py

and no such file or directory is found. I understand this is a extremely basic task, I have looked over the forums and web and I couldn't find any information, I guess that is because it is SO basic. Any help would be greatly appreciated.

MrSlinkyfish
Posts: 8
Joined: Sat May 19, 2012 6:18 pm
by domesday » Fri May 25, 2012 7:33 pm
If you are in the directory home/pi/ then type
Code: Select all
python name.py
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK
by itimpi » Fri May 25, 2012 7:35 pm
Normally the command would be something like
python name.py
but I do not have a system to check this on.

Typing python with no parameters should load the python interpreter in interactive mode.
Posts: 1035
Joined: Sun Sep 25, 2011 11:44 am
Location: Potters Bar, United Kingdom
by MrSlinkyfish » Fri May 25, 2012 7:43 pm
Thank you for your quick responses.

I did as Domesday said, typed python name.py and it worked.

Thanks again
Posts: 8
Joined: Sat May 19, 2012 6:18 pm
by domesday » Fri May 25, 2012 7:45 pm
To explain how it works, python is the software being loaded and then name.py the file being loaded into python.
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK
by andyl » Fri May 25, 2012 9:07 pm
MrSlinkyfish wrote:Thank you for your quick responses.

I did as Domesday said, typed python name.py and it worked.


This does work but it isn't the usual way you will do things. You can make it so you can just type a relative path (as you initially tried) OR just the name if you put it in a directory in your PATH.

Firstly the file needs to made executable. To do that we use the chmod command.
When you are in the directory with the file type
Code: Select all
chmod +x name.py


Secondly, you need a special comment at the top of the file. So edit name.py and make sure that the first line reads
Code: Select all
#!/usr/bin/python


Now you should be able to type name.py to execute your program (assuming you are in that directory).
Posts: 265
Joined: Tue Jan 10, 2012 11:05 am
by focusheart » Sat May 26, 2012 3:39 pm
andyl wrote:Now you should be able to type name.py to execute your program (assuming you are in that directory).


Code: Select all
./name.py

If you are in the folder, " ./ " should be typed. :)
Posts: 7
Joined: Mon Apr 09, 2012 5:04 am
by simonlc » Sun May 27, 2012 8:53 am
focusheart wrote:
andyl wrote:Now you should be able to type name.py to execute your program (assuming you are in that directory).


Code: Select all
./name.py

If you are in the folder, " ./ " should be typed. :)

Not if the folder is in your PATH.

Also if you have the hashbang (#!/usr/bin/env python) at the top of your file, you can omit the .py from the file name.
User avatar
Posts: 31
Joined: Wed May 16, 2012 4:11 am
Location: Calgary, AB
by spock » Sun May 27, 2012 8:56 am
how can you add it to our path?
Posts: 50
Joined: Sun Oct 02, 2011 10:33 am
by andyl » Sun May 27, 2012 9:58 am
spock wrote:how can you add it to our path?


Take a look at ~/.bash_profile

I still haven't got my Pi up and running out all day at a music festival yesterday and will be again today but I think the standard Debian profile is set up to add ~/bin if it exists. You can just copy those lines and change the directory if you like

For example if you had a python directory where you put executable programs you code do the following.

Code: Select all
if [ -d ~/python ] ; then
   PATH=~/python:$PATH
fi


Note it is possible to add you current directory to your PATH however it is generally considered bad practice.
Posts: 265
Joined: Tue Jan 10, 2012 11:05 am
by spock » Sun May 27, 2012 11:09 am
thanks! :)

why is it considered bad practice to add the current path?

if i remember correctly some distros (but this was many years ago) added it by default.
Posts: 50
Joined: Sun Oct 02, 2011 10:33 am
by andyl » Sun May 27, 2012 3:22 pm
spock wrote:thanks! :)

why is it considered bad practice to add the current path?

if i remember correctly some distros (but this was many years ago) added it by default.


Security. If you do put it in your path make sure you put it at the end.

If at the start then you may expect to run a normal command but find you are running something unexpected as you are in a directory with a file with the same name as the command you want to run.

If at the end then you aren't going to end up with user programs masking system commands, but someone could still create a file called sl (or some other common typo) for example that could do anything they wanted - like steal your files or wipe your system.

If you are on something which you know is a single user system (which I guess most Raspis will be it isn't too bad. But if you are used to using multi-user unix/linux systems then it could be disastrous.
Posts: 265
Joined: Tue Jan 10, 2012 11:05 am