joefly
Posts: 64
Joined: Thu May 31, 2012 4:10 pm

Very Basic Python Questions

Thu May 31, 2012 4:15 pm

Hi
Last year, I started playing with python to integrate with Arduino. It has been a while since I touched it and it was on a basic level. So I feel like I am back to the very beginning.

I just got by RPi, and new to linux and basically python.

My questions are:
1) Is python native on the Debian distro, if so, how to access the programming environment? If not, how do I install it on the Debian.?


I tried searching around to find the answers but the question seems to be too basic..

Thanks

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Very Basic Python Questions

Thu May 31, 2012 4:56 pm

The latest Debian image comes with Python 2.6 already installed.

Python 2.7 is the latest, so you might want to upgrade it. But if you did, you would have to compile it from source. That's generally pretty simple, but I've not tried it on the RaspPi yet. I'll give it a go in the next few days and report back.

yeahbox
Posts: 36
Joined: Sun May 27, 2012 10:35 pm
Location: Manchester, UK

Re: Very Basic Python Questions

Thu May 31, 2012 5:35 pm

joefly wrote: 1) Is python native on the Debian distro, if so, how to access the programming environment? If not, how do I install it on the Debian.?
Yes, it is already installed. When you have logged in and you are at the console prompt write

Code: Select all

pi@raspberrypi:~$ python
and now you are in the most basic environment. Try:

Code: Select all

>>> a = 45
>>> b = 2
>>> c = a+b
>>> print c
47
>>> print "Hello world!"
Hello world!
>>> 
When you are done testing and want to exit the environment write

Code: Select all

>>> exit()
The next step is to make a little script instead. Try:

Code: Select all

pi@raspberrypi:~$ nano test.py
Now write a little test script

Code: Select all

def Add(a, b):
    result = a+b
    return result

print Add(4, 5)
print Add("cool", "stuff")
now press ctrl-x to save, press Y and Enter. To run write python test.py

Code: Select all

pi@raspberrypi:~$ python test.py
9
coolstuff
pi@raspberrypi:~$

joefly
Posts: 64
Joined: Thu May 31, 2012 4:10 pm

Re: Very Basic Python Questions

Thu May 31, 2012 7:24 pm

YeahBox!!! That is exactly what I needed!! So easy and basic but yet not obvious.

Even now I know how to run my previously saved program on a PC. Thank you again, I am sure I will be back with hopefully more advanced questions.

Reider
Posts: 75
Joined: Sun Mar 04, 2012 12:00 pm

Re: Very Basic Python Questions

Fri Jun 01, 2012 9:33 am

Many thanks. Its also good to see that when the GUI is started with startx the test.py or any Python file can easilly be found and run in there too.

Steve

joefly
Posts: 64
Joined: Thu May 31, 2012 4:10 pm

Re: Very Basic Python Questions

Sun Jun 03, 2012 4:27 am

yeahbox, i am making great progress. I figured out how to import python extensions and figured out how to connect to arduino. I also tried updating by python by using apt-get install python, but it responds that it is allready the latest version

is there a way to upgrade to 2.7.3 because the program that i previously wrote was on 2.7.3 and now the error message I am getting is something serialposix.py raised TypeError ('expected str, got %s' % type(data))

the program works on my win machine with python 2.7.3, the only thing I can think of is the difference in python version or serial extension version. How do I manually download and install this on RPi

Thanks

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: Very Basic Python Questions

Sun Jun 03, 2012 5:39 am

I have compiled Python on my Pi. It is fairly straightforward.
Download the version you want, eg 2.7.3
Unpack the tarball and open a terminal in the Python-2.7.3 directory.
Then:

Code: Select all

./configure
make
make install
If you want to use pygame with your new Python, download pygame.
Then in the un-packed directory:

Code: Select all

python2.7 config.py
python2.7 setup.py build
python2.7 setup.py install
You might need to use "sudo" for this last instruction.

By the way, the Educational release is going to use Python 3. It might be worth thinking about moving on to this now. I use Python 3.1.4 as that's the latest version for which pygame works.
mark

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Very Basic Python Questions

Sun Jun 03, 2012 9:40 am

antiloquax wrote: You might need to use "sudo" for this last instruction.
In general you have to use "sudo" for both of the "install" lines, but you should not use them for any of the others. If you use them for the others you will end up with files owned by the superuser in your home directory. That is a pain. You will also be running lots of scripts as the superuser. That may be a security hole.

User avatar
Robert_M
Posts: 211
Joined: Fri Nov 25, 2011 12:50 am

Re: Very Basic Python Questions

Sun Jun 03, 2012 10:48 pm

yeahbox wrote:
joefly wrote: 1) Is python native on the Debian distro, if so, how to access the programming environment? If not, how do I install it on the Debian.?
Yes, it is already installed. When you have logged in and you are at the console prompt write

Code: Select all

pi@raspberrypi:~$ python
and now you are in the most basic environment. Try:

Code: Select all

>>> a = 45
>>> b = 2
>>> c = a+b
>>> print c
47
>>> print "Hello world!"
Hello world!
>>> 
When you are done testing and want to exit the environment write

Code: Select all

>>> exit()
The next step is to make a little script instead. Try:

Code: Select all

pi@raspberrypi:~$ nano test.py
Now write a little test script

Code: Select all

def Add(a, b):
    result = a+b
    return result

print Add(4, 5)
print Add("cool", "stuff")
now press ctrl-x to save, press Y and Enter. To run write python test.py

Code: Select all

pi@raspberrypi:~$ python test.py
9
coolstuff
pi@raspberrypi:~$
This was *very* helpful - thanks for posting it! :-)
I sometimes ride my Pi to the Forum.

joefly
Posts: 64
Joined: Thu May 31, 2012 4:10 pm

Re: Very Basic Python Questions

Mon Jun 04, 2012 4:13 am

Antiloquax. Thanks that was helpful to figuring how to upgrade. I got 2.7.3 going. Unfortunately. I figured out it was not the version problem. It was the RPi was not sufficiently powering my arduino which has an LCD attached so the Arduino was not acting correctly.

Thanks anyway for the help

yeahbox
Posts: 36
Joined: Sun May 27, 2012 10:35 pm
Location: Manchester, UK

Re: Very Basic Python Questions

Mon Jun 04, 2012 8:46 am

joefly wrote:Antiloquax. Thanks that was helpful to figuring how to upgrade. I got 2.7.3 going. Unfortunately. I figured out it was not the version problem. It was the RPi was not sufficiently powering my arduino which has an LCD attached so the Arduino was not acting correctly.
Good that you are up and running. You also figured out that it wasn't a version issue, which is what I expected. There is a bigger difference between python 2.x and 3.x than it is between 2.6 and 2.7. There is already plenty of discussions about which version of python one "should" run. I am personally sticking to 2.7 for my work since I am depending on many external libraries that yet isn't supported by 3.x. But if you are picking up your skills again basically from scratch then I would recommend 3.x because it is the future as others have said. Keep in mind that external libraries (like PySerial) always needs to match whatever version you are using.

Return to “General programming discussion”