Author Topic: Python version  (Read 5489 times)

fosamax (OP)

  • Posts: 327
Python version
« on: August 07, 2013, 10:17:10 am »
What's the version number for the builded in python on gcw zero (since Python 3.x is not backward compatible with Python 2.x) ?

pcercuei

  • Posts: 1667
    • My devblog
Re: Python version
« Reply #1 on: August 07, 2013, 05:33:53 pm »
We use Python 2.7.3 actually.

vonzippenstein

  • Posts: 49
    • vonzippenstein.com
Re: Python version
« Reply #2 on: November 03, 2013, 01:11:50 am »
Ran into some hurdles getting a basic python/pygame script running so I took the hardware test and boiled it down to a simple Hello World.
Thought I'd share it for anyone else just starting out.
Code: [Select]
#!/usr/bin/env python
 
import sys, time, pygame
import pygame.locals
 
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
 
# OpenDingux SDL button mappings
BTN_DPAD_UP = pygame.locals.K_UP
BTN_DPAD_DOWN = pygame.locals.K_DOWN
BTN_DPAD_LEFT = pygame.locals.K_LEFT
BTN_DPAD_RIGHT = pygame.locals.K_RIGHT
BTN_A = pygame.locals.K_LCTRL
BTN_B = pygame.locals.K_LALT
BTN_X = pygame.locals.K_SPACE
BTN_Y = pygame.locals.K_LSHIFT
BTN_START = pygame.locals.K_RETURN
BTN_SELECT = pygame.locals.K_ESCAPE
BTN_LEFT_SHOULDER = pygame.locals.K_TAB
BTN_RIGHT_SHOULDER = pygame.locals.K_BACKSPACE
BTN_HOLD = pygame.locals.K_PAUSE  # NOTE OpenDingux=hold_slide
 
def main(argv=None):
        if argv is None:
                argv = sys.argv
 
        pygame.init()
        pygame.mixer.init()
       
        # set up analog stick
        try:
                JOY = pygame.joystick.Joystick(0)
                JOY.init()
                print 'Initialized Joystick : %s' % JOY.get_name()
        except pygame.error:
                JOY = None
 
        # set up the screen/window
        screen = pygame.display.set_mode((320, 240), pygame.DOUBLEBUF)
        pygame.mouse.set_visible(False)
 
        # set up fonts
        font_text = pygame.font.SysFont(None, 20)
 
        running = True
        while running:
               
                text_surface = font_text.render("HELLO WORLD", True, WHITE,)
                textRect = text_surface.get_rect()
                textRect.centerx = screen.get_rect().centerx
                textRect.centery = screen.get_rect().centery
       
                screen.blit(text_surface, textRect)
                for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                                running = False  # Quit
                        elif event.type == pygame.KEYDOWN:
                                if event.key == BTN_SELECT:
                                        running = False  # Quit
           
                screen.blit(screen, (0, 0))
                pygame.display.flip()
   
        return 0
 
if __name__ == "__main__":
        sys.exit(main())
Save that as a .py file and drop it anywhere on your Zero then add a link in gmenu2x.
http://www.youtube.com/watch?v=FYK67N8Bwrc

vau0807

  • Posts: 15
Re: Python version
« Reply #3 on: December 30, 2013, 09:28:48 am »
Unfortunately, the Hello World example won't run on my gcw0. I saved the contents to a file called "gcw0hw.py", copied the file to my external SD card, and created a link in the Application section of GMenu2x. When I launch the "gcw0hw" Application item, there is a blank screen for about a second then back to the Application menu.

Are Python and PyGame pre-installed along with the Dingoonity kernel on the gcw0?

zear

  • * Moderator
  • Posts: 2381
Re: Python version
« Reply #4 on: December 30, 2013, 11:50:54 am »
Are Python and PyGame pre-installed along with the Dingoonity kernel on the gcw0?
They are installed with the rootfs, yes. Stringrolled that comes preinstalled is using PyGame, so if it works, PyGame is present.
Are you sure your python script was saved with the unix endlines?
What does the first line of your script look like?

zear

  • * Moderator
  • Posts: 2381
Re: Python version
« Reply #5 on: December 30, 2013, 11:55:21 am »
Are Python and PyGame pre-installed along with the Dingoonity kernel on the gcw0?
They are installed with the rootfs, yes. Stringrolled that comes preinstalled is using PyGame, so if it works, PyGame is present.
Are you sure your python script was saved with the unix endlines?
What does the first line of your script look like?

Edit:
Quote
Unfortunately, the Hello World example won't run on my gcw0.
It works perfectly here, which hints me to the theory you saved the file with windows endlines, essencially turning it into a one long line.

vau0807

  • Posts: 15
Re: Python version
« Reply #6 on: December 30, 2013, 07:57:23 pm »
Are you sure your python script was saved with the unix endlines?

You called it  ;D
I re-opened the script in Notepad++, selected Edit -> EOL Conversion -> Unix/OSX Format, saved the script, and now it run fine on the gcw0. Also, the script already ran fine on Python 2.7.3 for Windows. Notepad++ is popular as a lightweight IDE for Windows scripting and development. I'll have to remember to do the EOL Conversion step prior to copying the py files to the gcw0.

zear

  • * Moderator
  • Posts: 2381
Re: Python version
« Reply #7 on: December 30, 2013, 08:25:22 pm »
Python itself might be able to handle Windows line endings, however the first line of the python script:
Code: [Select]
#!/usr/bin/env pythonis used by the OS to evaluate what program to call to execute the content of the file.
If this line doesn't contain the proper path to python (but instead the entire length of the code :D), python will never be executed to parse your script.

peace_hippie

  • Posts: 3
Re: Python version
« Reply #8 on: November 05, 2015, 07:35:35 am »
Thanks to everyone for posting the above. It was really helpful. In my case I also needed to make the script executable (chmod +x) before it would work.