Python modules

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
liude
Newbie
Posts: 16
Joined: Fri Dec 18, 2009 9:42 am
Contact:

Python modules

#1 Post by liude »

Was trying to make a script that checks online for updates to the main script file, and it works fine untill i try to run it without an internet connection. It crashes, returnin a syntax error on the module im using. Problem is, its a module distributed with python itself, so its supposed to work flawlessly.

Module name: urllib2

Cant post code since im posting from mobile.

fortaat
Regular
Posts: 183
Joined: Tue May 18, 2010 1:16 pm
Contact:

Re: Python modules

#2 Post by fortaat »

Could you post the error?
Does the error occur when you import the module, or when you use its functions?

Regardless, a dirty but effective solution would be:

Code: Select all

python:
	import urllib2
	try:
		urllib2.funtion
		urllib2.psuedo_code

	except:
		print "Try again later when you have internet connection"
This will be good as long as you won't have another unrelated error in the "try" block.

Incendium
Regular
Posts: 25
Joined: Wed Dec 09, 2009 12:57 am
Contact:

Re: Python modules

#3 Post by Incendium »

I don't think urllib2 is bundled with Ren'Py by default. You should be able to drop in a copy from a real Python install though, just make sure it is the same version (2.6) and you grab any dependencies the module requires that Ren'Py doesn't include by default.

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Python modules

#4 Post by PyTom »

It's bundled with Ren'Py, but only in the latest versions.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

liude
Newbie
Posts: 16
Joined: Fri Dec 18, 2009 9:42 am
Contact:

Re: Python modules

#5 Post by liude »

Did it, it crashes with the same error but on httplib (one of the modules urllib2 imports), but this time with a client error and log file.

Log says "error on httplib:line 74: 'with' becomes a reserved keyword on python 2.6".

Any help? I used the same base code as provided.

Edit: im using modules copied from a python install, as well as the latest ren'py version.

fortaat
Regular
Posts: 183
Joined: Tue May 18, 2010 1:16 pm
Contact:

Re: Python modules

#6 Post by fortaat »

If I remember correctly, urllib2 doesn't import httplib, you should do the import statement yourself.

What version of python are you using?

What you reported is very weird since although it should produce a client error and log file, it shouldn't crash. Did it get to the print statement?

Once you get home, post the code.

liude
Newbie
Posts: 16
Joined: Fri Dec 18, 2009 9:42 am
Contact:

Re: Python modules

#7 Post by liude »

This is the code. Ive replaced the URL since its a private update server, so lets just call it whatever we know doesn't exist:

Code: Select all

label update_checker:
    $ newversion = ""
    $ currentversion = persistent.currentversion
    $ updates = ""
    $ connected = "no"
    scene LightGray
    $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5)
    $ ui.textbutton("Connecting to update server...", clicked = None, yminimum=130)
    $ renpy.pause(0.4)
    python:
        import urllib2
        try:
            urllib2.urlopen('http://myprogramupdateserver.com/currentversion.htm')
            connected = "yes"
        except:
            connected = "no"
    if connected is "yes":
        scene LightGray
        $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5, xminimum=140)
        $ ui.textbutton("Connected!", clicked = None, yminimum=130)
        $ renpy.pause(0.4)
        $ ui. window(xfill=False, yfill=False, xalign=0.5, yalign=0.5, xminimum=140)
        $ ui.textbutton("Checking for updates...", clicked = None, yminimum=130)
        $ renpy.pause(0.4)
        python:
            try:
                for line in urllib2.urlopen('http://myprogramupdateserver.com/currentversion.htm'):
                    if currentversion in line:
                        updates = 1
            except:
                updates = 0
        if updates == 1:
            $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5, xminimum=140)
            $ ui.vbox()
            $ ui.textbutton("No new updates available.", clicked = None, yminimum=110)
            $ ui.textbutton("Ok", clicked=ui.callsinnewcontext("_main_menu"), xalign=0.5)
            $ ui.close()
            $ choice = ui.interact(suppress_overlay=True)
        else:
            $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5, xminimum=140)
            $ ui.textbutton("There is a new update available. Install?", clicked = None, yminimum=130)
            $ choice = ui.interact(suppress_overlay=True)
    elif connected is "no":
        scene LightGray
        $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5)
        $ ui.vbox()
        $ ui.textbutton("Failed to connect to update server.", clicked = None) 
        $ ui.textbutton("Ok", clicked=ui.callsinnewcontext("_main_menu"), xalign=0.5)
        $ ui.close()
        $ choice = ui.interact(suppress_overlay=True)
The updater is a separate script from the main program, "updater.rpy", so that it can download and remove old files and install newer install newer ones without crashing. I still haven't made the download and install code, but i know the concepts so ill make it after this code which is meant to check first if a connection to the server is available and then if there is a new update.
persistent.currentversion is defined on script.rpy with other init code, so when a new version of the script is installed it will be different, as you have to go through a menu defined in said script to reach the update checker, currently

Code: Select all

init:
    python:
        persistent.currentversion = """				<h3 class="fw-title"><a name="653183884"></a><!-- ParagraphTitleStart -->Current Version: 1.0.0 <!-- ParagraphTitleEnd --></h3>
"""
Seems to work on my 64-bit windows 7 pc, glitches on my windows XP 32 bit netbook...

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Python modules

#8 Post by PyTom »

What version of renpy are you using? And are you using the version of httplib that comes with renpy, or are you including one as part of your game?

From my phone.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

liude
Newbie
Posts: 16
Joined: Fri Dec 18, 2009 9:42 am
Contact:

Re: Python modules

#9 Post by liude »

Im using the version that comes with the official python installation from http://www.python.org. Using renpy 6.10.2 in both pcs.

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Python modules

#10 Post by PyTom »

In the 6.11 series, compatible versions of urllib and urllib2 are included with Ren'Py, and so you shouldn't include them as part of your game. For older versions, it's up to you to work out compatibility issues.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

liude
Newbie
Posts: 16
Joined: Fri Dec 18, 2009 9:42 am
Contact:

Re: Python modules

#11 Post by liude »

ok, ill upgrade my version then... just hope the other scripts dont glitch (i started the game before 6.10 was released).

liude
Newbie
Posts: 16
Joined: Fri Dec 18, 2009 9:42 am
Contact:

Re: Python modules

#12 Post by liude »

Ok, upgraded it and the code seems to work fine now. If anyone wants to see the entire code, feel free to use it however you want, credit is appreciated but not required.

Code: Select all

label update_checker:
# Define variables to be used.
    $ newversion = ""
    $ currentversion = persistent.currentversion
    $ updates = ""
    $ connected = "no"
# Changes scene to create a new window.
    scene whatever_your_bg_is
    $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5)
#Displays connecting to update server message.
    $ ui.textbutton("Connecting to update server...", clicked = None, yminimum=130)
    $ renpy.pause(0.4)
# Python code imports urllib2 module and checks if your update server/website is online
    python:
        import urllib2
        try:
            urllib2.urlopen('http://myprojectwebsite.com')
            connected = "yes"
        except:
            connected = "no"
    if connected is "yes":
# Reports that the server could be reached and is checking for updates.
        scene LightGray
        $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5, xminimum=140)
        $ ui.textbutton("Connected!", clicked = None, yminimum=130)
        $ renpy.pause(0.4)
        $ ui. window(xfill=False, yfill=False, xalign=0.5, yalign=0.5, xminimum=140)
        $ ui.textbutton("Checking for updates...", clicked = None, yminimum=130)
        $ renpy.pause(0.4)
# Python code checks the url provided for the new version. A .txt file is better for this as it holds simple lines and that will be usefull later during download url checking.
        python:
            try:
                for line in urllib2.urlopen('http://myprojectwebsite.com/currentversion.txt'):
                    if currentversion in line:
                        updates = 1
            except:
                updates = 0
        if updates == 1:
# Value contained on the currentversion variable is reported to exist on the file checked. The ui reports that there are no updates available.
            $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5, xminimum=140)
            $ ui.vbox()
            $ ui.textbutton("There are no updates available.", clicked = None, yminimum=110)
            $ ui.textbutton("Ok", clicked=ui.callsinnewcontext("_main_menu"), xalign=0.5)
            $ ui.close()
            $ choice = ui.interact(suppress_overlay=True)
        else:
# Value contained on the currentversion variable does not exist on the website. The ui reports that a new update is available and prompts the user to download it.
# When the user chooses to download and install it the script jumps to the specified label.
            $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5, xminimum=140)
            $ ui.vbox()
            $ ui.textbutton("There is a new update available.", clicked = None, yminimum=130)
            $ ui.hbox(xalign=0.5)
            $ ui.textbutton("Update", clicked = ui.jumps("update_installer"), xalign=0.5)
            $ ui.textbutton("Cancel", clicked = ui.callsinnewcontext("_main_menu"), xalign=0.5)
            $ ui.close()
            $ ui.close()
            $ choice = ui.interact(suppress_overlay=True)
# If connection failed on the first step, the ui reports that it failed to connect to the server/website.
    elif connected is "no":
        scene LightGray
        $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5)
        $ ui.vbox()
        $ ui.textbutton("Failed to connect to the server.", clicked = None) 
        $ ui.textbutton("Ok", clicked=ui.callsinnewcontext("_main_menu"), xalign=0.5)
        $ ui.close()
        $ choice = ui.interact(suppress_overlay=True)

        
# If the user chooses to download and install the updates this label is called.
label update_installer:
# Python code imports necessary modules and checks for the expression "http://" on the currentversion.txt online file.
# The line containing said code is then treated as the download url and the file is downloaded and saved to the "game" directory inside the game folder.
# Note1: the code provided will download the "script.rpy" file from the url provided in the .txt file. To download other files the same code is used, but "filename" should be changed to the file you 
# want to download. To change the directory which the file is downloaded to change the arguments in os.chdir to the name of an existing folder inside the project directory, or the full path to
# a folder outside it.
# Note2: if you try to run the code before building distributions of the game, the script will crash as the game is being run from the "ren'py" directory, and there is no "game" directory in it.
    python:
        import urllib
        import os
        os.chdir("game")
        for line in urllib2.urlopen('http://myprojectwebsite.com/currentversion.txt'):
            if "http://" in line:
                url = line
                filename = "script.rpy"
                urllib.urlretrieve(url, filename=filename)
    $ ui.window(xfill=False, yfill=False, xalign=0.5, yalign=0.5)
    $ ui.vbox()
    $ ui.textbutton("Update complete. Ok to return to main menu.", clicked = None, xalign=0.5, yminimum=130)
    $ ui.textbutton("OK", clicked=ui.callsinnewcontext("_main_menu"), xalign=0.5)
    $ ui.close()
    $ choice = ui.interact(suppress_overlay=True)
The currentversion variable should be set either in a separate file and jumped to before this code is run, in which case the file containing it should be updated as well, or in the file to be updated providing its run before this code (this can be options.rpy, but that could generate issues of being able to update indefinitely until the program is restarted).

This code should be in a file separate from the main game files, since replacing it during the update could cause the code to remain unfinished. This can of course be avoided by creating a temporary directory and downloading the new files there then moving to the official one, but even then the script that moves the file would have to be static (can't update).

fortaat
Regular
Posts: 183
Joined: Tue May 18, 2010 1:16 pm
Contact:

Re: Python modules

#13 Post by fortaat »

Thanks for sharing.

liude
Newbie
Posts: 16
Joined: Fri Dec 18, 2009 9:42 am
Contact:

Re: Python modules

#14 Post by liude »

Im gonna ask for a little bit more help in this topic.

When trying the improbable for debugging purposes, i closed the windo during update check, just after control was returned to me after it requested the website. It closed with an error message and a log file in the renpy default folder, no traceback from jedit but some lines on that log. Here is the full error log. Im not sure what caused it, i think its leftover data from the requested url, but here is the log. I have replaced the file location with *, leaving only the path displayed inside the renpy folder.

Code: Select all

*\renpy\python.py:74: RuntimeWarning: use sndarray: no module named numpy or Numeric found
*\renpy\python.py:74: RuntimeWarning: use movie: No module named movie
*\renpy\python.py:74: RuntimeWarning: use scrap: No module named scrap
*\renpy\python.py:74: RuntimeWarning: use mixer: No module named mixer
*\renpy\python.py:74: RuntimeWarning: use surfarray: No module named surfarray
*\renpy\python.py:74: RuntimeWarning: use numpyarray: No module named numpyarray
*\renpy\python.py:74: RuntimeWarning: use font: No module named font
Exception in thread Thread-1:
Traceback (most recent call last):
  File "threading.pyo", line 532, in __bootstrap_inner
  File "threading.pyo", line 484, in run
  File "*\renpy\loadsave.py", line 326, in autosave_thread
  File "*\renpy\loadsave.py", line 158, in save
  File "*\renpy\loadsave.py", line 47, in dump
PicklingError: Can't pickle <type 'module'>: attribute lookup __builtin__.module failed
If anyone could help me clear this up...

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot], Semrush [Bot]