OSX application built with py2app can't see bundled PySide module?
I'm trying to deploy a Python app on OSX that was built with PySide. py2app packages it without issue, copying and linking a lot of PySide and Qt files in the process. But then, when I try to run the built app, I get this error: Traceback (most recent call last): File /Users/sequence/Desktop/code/dailies/dist/dailies_v02.app/Contents/Resources/__boot__.py, line 31, in module _run('dailies_v02.py') File /Users/sequence/Desktop/code/dailies/dist/dailies_v02.app/Contents/Resources/__boot__.py, line 28, in _run execfile(path, globals(), globals()) File /Users/sequence/Desktop/code/dailies/dist/dailies_v02.app/Contents/Resources/dailies_v02.py, line 9, in module from PySide.QtCore import * File PySide/__init__.pyc, line 2, in module File PySide/private.pyc, line 2, in module File PySide/QtCore.pyc, line 18, in module File PySide/QtCore.pyc, line 15, in __load ImportError: '/usr/lib/python2.6/lib-dynload/PySide/QtCore.so' not found The weird thing is, QtCore.so IS included in the application bundle: py2app copied it to the build under Contents/Resources/lib/python2.6/lib-dynload/PySide/. Is there a reason the application isn't seeing this? -- http://mail.python.org/mailman/listinfo/python-list
Including a remote file -- permission denied?
I have a Python script running on the default OSX webserver, stored in /Library/WebServer/CGI-Executables. That script spits out a list of files on a network drive, a la os.listdir('/Volumes/code/ directory/'). If I just execute this from the terminal, it works as expected, but when I try to access it through a browser (computer.local/cgi-bin/test.py), I get a permissions error (type 'exceptions.OSError': [Errno 13] Permission denied: '/Volumes/code/ directory'). Is there any way to give the script permission to access the network when accessed via CGI? -- http://mail.python.org/mailman/listinfo/python-list
Problem with arrays in a recursive class function
I have a list of nodes, and I need to find a path from one node to another. The nodes each have a list of nodes they are connected to, set up like this: class Node(object): def __init__(self, connectedNodes): self.connectedNodes = connectedNodes nodes = { 1: Node([4]), 2: Node([3]), 3: Node([2, 4, 5]), 4: Node([1, 6, 3]), 5: Node([3, 7]), 6: Node([4, 9]), 7: Node([5, 8]), 8: Node([7, 9]), 9: Node([6, 8]) } I made a quick brute-force pathfinder to solve it (in this case, a path from node 1 to node 9). Here it is: class PathFind(object): def __init__(self, source, destination): self.source = source self.destination = destination self.solved = [] def Search(self): self.PathFind([self.source]) if self.solved: print Solutions: for i in self.solved: print \t + str(i) else: print Couldn't solve. def PathFind(self, trail): location = trail[-1] if location == self.destination: self.solved.append(trail) print Solution found: + str(trail) else: possibilities = [] for i in nodes[location].connectedNodes: if not i in trail: possibilities.append(i) for i in possibilities: trail.append(i) self.PathFind(trail[:]) if not possibilities: print Dead end: + str(trail) finder = PathFind(1, 9) finder.Search() Unfortunately, it doesn't seem to be giving me the result I was after. This is the output: Solution found: [1, 4, 6, 9] Dead end: [1, 4, 6, 3, 2] Solution found: [1, 4, 6, 3, 2, 5, 7, 8, 9] Solutions: [1, 4, 6, 9] [1, 4, 6, 3, 2, 5, 7, 8, 9] The problem is the array trail[], which seems to survive from instance to instance of PathFind(). I thought that by calling self.PathFind (trail[:]), I was creating a new copy of trail[], but obviously something isn't running like I expected. Is there something I'm misunderstanding here, or is there just a stupid bug in my code I haven't caught? -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with arrays in a recursive class function
Never mind -- ditched the attempt and implemented Dijkstra. -- http://mail.python.org/mailman/listinfo/python-list
Pickling classes -- disappearing lists?
I'm trying to pickle an instance of a class. It mostly works just fine -- I can save the pickle to a file, restore it from that file, and it's mostly okay. The problem is, some lists seem to disappear. For example (snipped and crunched from the code giving me trouble): --- class InitGame: value = False journal = [] game.InitGame() def Save(): global game import cPickle, gzip, os # Change some data game.journal.append(A value) game.value = True pickles = {'game': game} jar = gzip.open(pickefile, 'wb') cPickle.dump(pickles, jar, 2) jar.close() def Load(): global game import gzip, os, cPickle jar = gzip.open(picklefile, 'r') loaded = cPickle.load(jar) jar.close() game = loaded[game] --- Now, if I save a pickle, then load it back in, I'll get an instance of InitGame called game, and game.value will be true, but the list journal will be empty. Am I missing something obvious about the pickle spec? Am I doing something wrong? Or should I be hunting for a deeper bug in the code? I've noticed that pretty much anything that's a list isn't saving to the pickle (or loading from the pickle... it's hard to tell which). -- http://mail.python.org/mailman/listinfo/python-list
Re: Memory error due to big input file
BTW, you should derive all your classes from something. If nothing else, use object. class textfile(object): Just out of curiousity... why is that? I've been coding in Python for a long time, and I never derive my base classes. What's the advantage to deriving them? -- http://mail.python.org/mailman/listinfo/python-list
Re: Pickling classes -- disappearing lists?
Your class definition isn't right. It makes 'value' and 'journal' class variables (Java lingo: static variables) as opposed to the instance variables they should be. Here's a corrected version: Woah, thanks. I can't believe I made such a stupid mistake. It's not like I'm a newcomer to Python, either. I'm can't believe I never noticed what I was doing. No more 2am coding for me. Thanks, Aaron -- http://mail.python.org/mailman/listinfo/python-list
Using Python for file packing
I'm working on a Python application right now that uses a large number of audio assets. Instead of having a directory full of audio, I'd like to pack all the audio into a single file. Is there any easy way to do this in Python? My first instinct was to attempt to pickle all the audio data, but something tells me that that experiment would only end in tears. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using Python for file packing
Do you mean like a zip or tar file? http://docs.python.org/library/zipfile.htmlhttp://docs.python.org/library/tarfile.html I had no idea you could access a single file from a ZIP or TAR without explicitly extracting it somewhere. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Module caching
Anyway, this person also posted on mod_python list. One of the things I highlighted there was that mod_python for some configurations is multithreaded and as such they may not be properly protecting variables if they are storing them at global scope. They haven't responded to any comments about it on mod_python list. They were also told to read: http://www.dscpl.com.au/wiki/ModPython/Articles/TheProcessInterpreter... Graham Thanks, Graham. Sorry I didn't respond -- it's crunch time here, so I was pulled off the project for a couple days to help with something else. I just didn't have time to catch up. In the end, we decided to convert everything from mod_python to CGI, which ended up getting us the functionality we were looking for. For development, MaxRequestsPerChild was already set to 1, but this didn't solve any of the variable caching issues. Aaron -- http://mail.python.org/mailman/listinfo/python-list
Re: Module caching
req.write(str(lab.game.settings.daemons)) del lab.game.settings req.write(str(lab.game.settings.daemons)) lab.game.settings = lab.game.InitGame() req.write(str(lab.game.settings.daemons)) Sorry, that should have been: req.write(str(lab.game.game.daemons)) del lab.game.game try: req.write(str(lab.game.game.daemons)) except: req.write(failed) lab.game.game = lab.game.InitGame() req.write(str(lab.game.game.daemons)) -- http://mail.python.org/mailman/listinfo/python-list
Best way to pickle functions
I have a number of functions that I need to pickle without necessarily knowing their names in advance. My first thought was to put all the functions in a class, then pickle the class, but it doesn't really work like I expected it to. import cPickle class PickleClass: def Awesome(self): pass stored = cPickle.dumps(PickleClass) del PickleClass restored = cPickle.loads(stored) Results: Traceback (most recent call last): File pickletest.py, line 7, in module restored = cPickle.loads(stored) AttributeError: 'module' object has no attribute 'PickleClass' So, the class itself isn't being pickled, just an instance of it. This being the case, what's the best way to store these functions? Maybe dump the class to a string and pull them back with an exec statement when I need them? -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to pickle functions
Pickling the source code is much sturdier. It's very unlikely that the same code runs differently in different interpreters. It's much more likely that the same code runs the same, or not at all. Okay, I've run into another problem. I've saved the code to a string, so I can call it up when I need it. I want to keep these functions all together, though, so I'm pushing them into a dictionary when I execute it. It seems like when I put it in a dictionary, though, it messes up the scope of the functions contained within. For example: import cPickle def Main(): holder = {} functiontest = def PickleTest():\n\tprint cPickle exec functiontest in holder print holder[PickleTest]() Main() ... produces: Traceback (most recent call last): File pickletest.py, line 11, in module Main() File pickletest.py, line 9, in Main print holder[PickleTest]() File string, line 2, in PickleTest NameError: global name 'cPickle' is not defined Is there any way to do this so that the functions have access to the higher scope? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to pickle functions
Never mind. Solved the problem by putting the functions in a class and dumping that into a string. Then, when I need it, I executed the string to get myself the class, then created an instance of that class which gave me access to those functions along with the correct scope. Probably not the smartest solution, but it works for now. -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to pickle functions
Why not use import ? Simply recreate the source file, if necessary, and import it again. Ah, you'd think it would be that easy :P The problem with just importing a module is that the module is then cached in memory. Multiple copies of the program are running on a server, and each of them have something akin to a randomfunctions module. When the first program is accessed, it loads randomfunctions. When the second program is accessed, it uses the randomfunctions module already in memory, even though it doesn't contain the right functions. So, I have to pull in these functions dynamically. -- http://mail.python.org/mailman/listinfo/python-list
Module caching
Is there a way to make a Python app running in mod_python with zero persistence? I have an app that should be resetting its variables every time you access it, but sometimes -- and only sometimes -- the variables persist through a couple refreshes. They'll even persist through multiple browsers, so I know it's a Python issue and not a browser caching issue. Any assistance would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Module caching
Okay, I'm at my wit's end. I have a Python app, running via mod_python. There are variables in this app that, when changed, save their changes to a pickled file tied to a session ID. Then, when the page is accessed again, the variables are loaded from the respective file. But, when one user uses the page and a number of variables are changed, these changes persist, even if I try to load the saved values over them. So, if my Python file has a value of 5, the custom values file has a value of 10, but a user does something that changes the variable to 20, the next user who accesses the page with see the value as 20, even if their custom values file tries to set it differently. If anyone has experience with mod_python, either drop me a message here or e-mail me at aaron(at)manlab.com. I'd really appreciate some help with this. -- http://mail.python.org/mailman/listinfo/python-list
Re: Module caching
Huzzah, another post. I just discovered that even physically deleting the variable doesn't work. The module storylab.game has the class InitGame, which contains daemons = {}. A user runs the code, resulting in some values in daemons: {'berry2': , 'berry3': , 'berry1': }. These are pickled. The next user runs the code. I put this in to make sure daemons is getting reset: req.write(str(lab.game.settings.daemons)) del lab.game.settings req.write(str(lab.game.settings.daemons)) lab.game.settings = lab.game.InitGame() req.write(str(lab.game.settings.daemons)) Okay, that should wipe out any of the values and leave us with a clean slate, right? {'berry2': , 'berry3': , 'berry1': }failed{'berry2': , 'berry3': , 'berry1': } Oh, you'd be so lucky. Why? WHY? Why does these values persist? They persist if I change them, they persist if I delete them. Help... please :( -- http://mail.python.org/mailman/listinfo/python-list
Re: Module caching
are you an experienced python programmer? Yeah, I'd link to think I'm fairly experienced and not making any stupid mistakes. That said, I'm fairly new to working with mod_python. All I really want is to have mod_python stop caching variables. This seems like it should be easy enough to do, but I can't for the life of me find information on how to do it. Aaron -- http://mail.python.org/mailman/listinfo/python-list
Find the location of a loaded module
I'm running into a problem that's rapidly reaching keyboard-smashing levels. I'm trying to import a module into Python, but it seems like Python is almost randomly loading the module from an entirely different directory, one that shouldn't be in the module search path. When I tell Python to load a module, is there a way to tell which directory the module was loaded from? -- http://mail.python.org/mailman/listinfo/python-list
Re: Find the location of a loaded module
Okay, I'm going out of my mind. I have three directories -- 'act1', 'act2', and 'act3'. Each of these has a module named 'story'. Through mod_python, I need to load 'story' in the directory 'act1'. I do it like this: req.content_type = text/html sys.path.append(os.path.dirname( __file__ )) req.write(str(sys.path)) import story req.write(story.game.Save()) sys.path.pop() According to sys.path, these are Python's paths: ['/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/ plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib- dynload', '/usr/lib/python2.5/site-packages', '/usr/local/python2.5.2/ lib/python2.5/', '/home/www/---/docs/act1'] story.game.Save() returns the location of the story.game module, which is reported as '/home/www/---/docs/act1/story/game.pyc'. So far so good. Now, I'll try the same thing from the 'act2' directory. These are the paths reported in sys.path: ['/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/ plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib- dynload', '/usr/lib/python2.5/site-packages', '/usr/local/python2.5.2/ lib/python2.5/', '/home/www/---/docs/act2'] All good, right? Not so fast. Here's what story.game.Save() returns as its location: '/home/www/---/docs/act1/story/game.pyc'. Which means it's loading the 'story' module from the old location, even though that location is no longer in the path. If I refresh a couple times, eventually it loads the module from the proper directory ('act2'). Then, I'll go back to the first directory, and find that it too will be loading the module from 'act2'. I'll refresh, and it'll load from 'act1' again. I'll refresh a couple times, and suddenly it's loading from 'act2' again. I'm seriously going insane. If anyone has any insight, please, please share it with me. Aaron -- http://mail.python.org/mailman/listinfo/python-list
Re: Find the location of a loaded module
Here's another clue: if I'm trying to run the script from the directory 'act1', but it's loading the module from 'act2', if I rename the module directory in 'act2' and refresh, the module still reports that it's running from '/home/www/---/docs/act2/story/game.pyc'... even though that files no longer exists. If I refresh a few more times, both 'act1' and 'act2' will load the module from 'act1's directory (even though the directory doesn't appear in sys.path when you try to load it from 'act2'). So, Python is trying to import a module from a directory that isn't in sys.path, and will generally default to running the module from the directory where it was last run. If I run it from 'act1', then 'act2', both times it will load the module from 'act1'. If I do it the other way around, it will load the module from 'act2' both times. The question now is... why is it loading from a directory that isn't in sys.path? How can I avoid this? -- http://mail.python.org/mailman/listinfo/python-list
Re: Find the location of a loaded module
And more madness... Executed from inside 'act1', which contains the directory / module 'story': directory = os.path.dirname(__file__) req.write(str(directory)) story = apache.import_module('story', path=[directory]) Results: File /home/www/---/docs/act1/play.py, line 24, in Rebuild storylab = apache.import_module('story', path=[directory]) File /usr/local/python2.5.2/lib/python2.5/site-packages/mod_python/ importer.py, line 304, in import_module return __import__(module_name, {}, {}, ['*']) ImportError: No module named story Awesome. I'm going to go stick my head through a wall. -- http://mail.python.org/mailman/listinfo/python-list
Re: Find the location of a loaded module
Son of a bitch. It gets worse. Executed from inside 'act1', which contains the directory / module 'story': directory = os.path.dirname(__file__) req.write(str(directory)) story = apache.import_module('story', path=[directory]) Results: /home/www/---/docs/act1 File /home/www/---/docs/act1/play.py, line 24, in Rebuild storylab = apache.import_module('story', path=[directory]) File /usr/local/python2.5.2/lib/python2.5/site-packages/mod_python/ importer.py, line 304, in import_module return __import__(module_name, {}, {}, ['*']) ImportError: No module named story If I execute the exact same code from the 'act1' directory after running it from the 'act2' directory, it successfully loads the 'story' module... from 'act2'. Even though I used the Apache importer to specify the EXACT LOCATION of the module to import. 'req.write(str(os.path.dirname(__file__)))' returns '/home/www/---/ docs/act1'. 'req.write(story.game.Save())' returns '/home/www/--/docs/act2/ storylab/game.pyc' as the file being accessed. BLOODY HELL. Okay, deep breath. Does anyone know what's going on? Am I just not understanding how module importing in mod_python works? I'd really appreciate help, since I'll be stuck at work today until I can get this sorted out, and I've long since run out of ideas. -- http://mail.python.org/mailman/listinfo/python-list
Re: Find the location of a loaded module
'req.write(story.game.Save())' returns '/home/www/--/docs/act2/ storylab/game.pyc' as the file being accessed. Sorry, that should have read: 'req.write(story.game.Save())' returns '/home/www/--/docs/act2/story/game.pyc' as the file being accessed. -- http://mail.python.org/mailman/listinfo/python-list
Re: Find the location of a loaded module
And finally, an epilogue. So, the problem lies with how Python cached the modules in memory. Yes, the modules were in two different locations and yes, the one that I specified using its direct path should be the one loaded. The problem is, the module isn't always loaded -- if it's already in memory, it'll use that instead. And since the modules had the same name, Python wouldn't distinguish between them, even though they weren't exactly the same. So, loading the module act1/story would load act1/story. Then, loading the module act2/story would use the story module already in memory. Of course, this made the problem hard to pinpoint, since memory is a fickle thing, and the results weren't always reproducible. The final solution? Renaming the 'story' modules to 'story_1' and 'story_2'... and importing them via 'exec(from story_+modulename+ import game)'. Will I go to hell for this 'solution'? Probably. But hey, it means I can go home tonight instead of spending all evening at the office hitting my head against the wall. I'll come back to it Monday and try to figure out a more reasonable solution. -- http://mail.python.org/mailman/listinfo/python-list
Checking a string against multiple matches
I've been trying to read up on this, but I'm not sure what the simplest way to do it is. I have a list of string. I'd like to check to see if any of the strings in that list matches another string. Pseudocode: if two in [one, two, three, four]: return True Is there any built-in iteration that would do such a thing, or do I have to write a function to check for me? I was using .index on the list, but it would return True for strings that contained the search string rather than match it exactly, leading to false positives in my code. -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking a string against multiple matches
Damn you, Python, and your loose documentation! It never occurred to me to actually TRY my pseudocode, since I couldn't find anything on that type of statement. Anyway, feel free to ignore me from now on. -- http://mail.python.org/mailman/listinfo/python-list
Reading binary data
I've been trying to tackle this all morning, and so far I've been completely unsuccessful. I have a binary file that I have the structure to, and I'd like to read it into Python. It's not a particularly complicated file. For instance: signature char[3] GDE version uint32 2 attr_count uint32 { attr_id uint32 attr_val_lenuint32 attr_valchar[attr_val_len] } ... repeated attr_count times ... However, I can't find a way to bring it into Python. This is my code -- which I know is definitely wrong, but I had to start somewhere: import struct file = open(test.gde, rb) output = file.read(3) print output version = struct.unpack(I, file.read(4))[0] print version attr_count = struct.unpack(I, file.read(4))[0] while attr_count: print --- file.seek(4, 1) counter = int(struct.unpack(I, file.read(4))[0]) print file.read(counter) attr_count -= 1 file.close() Of course, this doesn't work at all. It produces: GDE 2 --- é --- ê Å I'm completely at a loss. If anyone could show me the correct way to do this (or at least point me in the right direction), I'd be extremely grateful. -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading binary data
signature, version, attr_count = struct.unpack('3cII', yourfile.read(11)) This line is giving me an error: Traceback (most recent call last): File test.py, line 19, in module signature, version, attr_count = struct.unpack('3cII', file.read(12)) ValueError: too many values to unpack -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading binary data
CORRECTION: '3cII' should be '3sII'. Even with the correction, I'm still getting the error. -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading binary data
Sorry, I had posted the wrong error. The error I am getting is: struct.error: unpack requires a string argument of length 12 which doesn't make sense to me, since I'm specifically asking for 11. Just for kicks, if I change the line to print struct.unpack('3sII', file.read(12)) I get the result ('GDE', 33554432, 16777216) ... which isn't even close, past the first three characters. -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading binary data
Taking everything into consideration, my code is now: import struct file = open(test.gde, rb) signature = file.read(3) version, attr_count = struct.unpack('II', file.read(8)) print signature, version, attr_count for idx in xrange(attr_count): attr_id, attr_val_len = struct.unpack('II', file.read(8)) attr_val = file.read(attr_val_len) print attr_id, attr_val_len, attr_val file.close() which gives a result of: GDE 2 2 1 4 é 2 4 ê Å Essentially, the same results I was originally getting :( -- http://mail.python.org/mailman/listinfo/python-list
mod_python and updated files
I have mod_python running on my server, but when I chance a Python file on the server, Apache needs to be restarted in order to have the changes take effect. I assume this is so mod_python can run persistently, but it's becoming quite a headache for development. Is there a way to turn off the persistence, or a way to force mod_python to compile and run the new version of the file? -- http://mail.python.org/mailman/listinfo/python-list
Using variables across modules
I'm having some trouble understanding how Python handles variables across multiple modules. I've dug through the documentation, but I still find myself at a loss. When you import a module, are you creating an instance of the variables within? For instance, if I have one file, variables.py, which contains myvar = 0, and I import it into both foo.py and bar.py with the line from variables import *, and then set myvar in foo.py and bar.py to different values, will each file have a different value for myvar? If so, how can I ensure that a change to myvar in bar.py is reflected by foo.py? Or am I completely off base? -- http://mail.python.org/mailman/listinfo/python-list
Re: Using variables across modules
Just wirte test code ! variables.py: myvar = 5 print myvar foo.py: from variables import * def PrintVar(): print myvar bar.py: from variables import * from foo import * print myvar myvar = 2 print myvar PrintVar() python bar.py output: 5 5 2 5 ... which is what I was expecting, but not what I want. Obviously, each import is creating its own instance of the variable. What I need is a way to change myvar from within bar.py so that PrintVar() returns the new value, even though it's in a different module. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using variables across modules
first read this to learn how objects and variables work in Python: http://effbot.org/zone/python-objects.htm and then read this to learn how from-import works, and when you're supposed to use it: http://effbot.org/zone/import-confusion.htm hope this helps! Awesome. After reading those two pages, I was able to correct the code and get things up and running. Thanks! Aaron -- http://mail.python.org/mailman/listinfo/python-list
Writing a function from within Python
Is it possible to change the content of a function after the function has been created? For instance, say I make a class: class MyClass: def ClassFunction(self): return 1 And I create an object: MyObject = MyClass() Is there any way to change the content of the function, a la pseudo- code: MyObject.ClassFunction = return 2 Thanks for any insight you might be able to offer. -- http://mail.python.org/mailman/listinfo/python-list