vinntec
Posts: 145
Joined: Thu Aug 02, 2012 9:37 am
Location: Basingstoke, UK

Running .py file from Python3

Thu Aug 16, 2012 7:46 am

Hi - does anyone know why it is if I run some programs under python3 directly all that happens is the window opens for a few seconds then closes - even though the same program works fine in IDLE3? Is there a log I need to be checking somewhere? Some other programs work fine under both python3 and idle3. Just wondering in case I am doing something silly. Peter

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

Re: Running .py file from Python3

Thu Aug 16, 2012 7:52 am

It might help if you can share your code.
Depending on what the program does, it might help to add:

Code: Select all

input()
To the end of your program - then the window should wait for you to press enter before quitting.

vinntec
Posts: 145
Joined: Thu Aug 02, 2012 9:37 am
Location: Basingstoke, UK

Re: Running .py file from Python3

Thu Aug 16, 2012 9:28 am

Sorry I wasn't being clear enough! I know about console applications, I am talking about programs made with Python3 + PyGame which run in XWindows where a panel comes up to do something, like show an animation (these are projects being prepared for my students). It is this window which opens and closes without anything happening. It seems to be connected with those which use an external file, such as image, for which I am using relative addresses? This is the simplest example - remember this works fine in IDLE3:

Code: Select all

import pygame, sys
from pygame.locals import *

pygame.init()

# set up the tick timer
FPS = 30  # frames per second
fpsClock = pygame.time.Clock()

# set up the window - drawing area = (0..399, 0..299)
DISPLAYSURF = pygame.display.set_mode((400,300),0,32)
pygame.display.set_caption('Cat Amimation')

# set up the display
WHITE = (255, 255, 255)
catImg = pygame.image.load('makinggames/cat.png')
catx = 10
caty = 10
direction = 'right'

# run the game loop
running = True
while running:
    # draw on the surface object
    DISPLAYSURF.fill(WHITE)
    if direction == 'right':
        catx += 5
        if catx == 280:
            direction = 'down'
    elif direction == 'down':
        caty +=5
        if caty == 220:
            direction = 'left'
    elif direction == 'left':
        catx -=5
        if catx == 10:
            direction = 'up'
    elif direction == 'up':
        caty -= 5
        if caty == 10:
            direction = 'right'

    DISPLAYSURF.blit(catImg, (catx, caty))
            
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
    pygame.display.update()
    fpsClock.tick(FPS)
    
pygame.quit()
        

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

Re: Running .py file from Python3

Fri Aug 17, 2012 2:37 pm

[quote="vinntec"]It seems to be connected with those which use an external file, such as image, for which I am using relative addresses? This is the simplest example - remember this works fine in IDLE3:

I've just tried this and when I changed the image file-name bit to "cat.png", (ie deleted "makinggames/" it worked.
mark

vinntec
Posts: 145
Joined: Thu Aug 02, 2012 9:37 am
Location: Basingstoke, UK

Re: Running .py file from Python3

Fri Aug 17, 2012 10:05 pm

antiloquax wrote:
vinntec wrote:It seems to be connected with those which use an external file, such as image, for which I am using relative addresses? This is the simplest example - remember this works fine in IDLE3:

I've just tried this and when I changed the image file-name bit to "cat.png", (ie deleted "makinggames/" it worked.
mark
Thanks. RPi's connections are being used by work laptop, so cannot fire it up at present. However sounds like it will work if in the same directory or maybe also if I give it a full path? /home/pi/Python/makinggames/cat.png (which is where the cat image is - program is in Python). I will give it a whirl later tonight. Peter

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Running .py file from Python3

Fri Aug 17, 2012 11:21 pm

@vinntec: Processes have a working directory and all relative paths used in a program are relative to that working directory. If you start a Python file by clicking on it in a desktop environment that desktop environment decides what the working directory for the process will be. So you either start the Python program from the command line — then you decide what the working directory is — or you make sure you build paths that are relative to the Python module. Every Python module has the name `__file__` defined which contains the path including the file name of that module. With the help of `os.path.dirname()` it is possible to get the directory part of that name. Then `os.path.join()` can be used to create a path to a subdirectory relative to the module file.

Code: Select all

import os
# ...
DATA_PATH = os.path.join(os.path.dirname(__file__), 'makinggames')
# ...
catImg = pygame.image.load(os.path.join(DATA_PATH, 'cat.png'))
A full path like ``/home/pi/Python/makinggames/cat.png`` is not portable and has to be changed if the data will be moved to another location.

Code: Select all

while not self.asleep():
    sheep += 1

vinntec
Posts: 145
Joined: Thu Aug 02, 2012 9:37 am
Location: Basingstoke, UK

Re: Running .py file from Python3

Sat Aug 18, 2012 6:12 am

Hi BJ - thanks for the advice. I will change my programs accordingly and check that sorts the problem. Obviously IDLE3 must make the current directory where the program was opened from or it would never work (which might not have been a bad thing). Peter

Return to “Python”