Weird import problem


I have a directory structure like NS/dir1/file1.py NS/dir2/file2.py if in the python shell I type import NS.dir1.file1 it works, however typing import NS.dir2.file2 fails with ImportError: No module named dir2.file2 Any ideas what could go wrong Directory permissions seem to ...
Posted On: Sunday 25th of November 2012 09:46:52 PM Total Views:  77
View Complete with Replies

RELATED TOPICS OF Python Programming PROGRAMMING LANGUAGE




So weird

Quote:I'm trying to learn Python, I installed Python 2.7.3 and for some reason, when I watch "TheNewBoston" video, I typed the same code as him, save as .py, i couldn't double click on the .py file to execute it. I right click and check the filetype is just "file". For example x = raw_input("Enter name here: ") print " " + x If I use notepad++ to save .py , I can double click, but when I type in the name and press enter, it closes... hope you understand my problem
VIEWS ON THIS POST

117

Posted on:

Monday 24th September 2012
View Replies!

python's YENC.DECODE -> weird output

I save posts from a midi music newsgroup, some are encoded with yenc encoding. This gave me an opportunity to try out the decoders in Python. The UU decoder works okay, but my YENC effort gives results unexpected: import yenc, sys fd1=open(sys.argv[1],'r') #yenc.encode(sys.argv[1],"outfile.yenc",bytes=0) yenc.decode(sys.argv[1],"outfile.mid",bytes=0,crc_in='') I confirmed that yenc.decode exactly reverses yenc.encode, BUT the encoding itself seems to differ from the USENET standard. That is, when I decode USENET files the result isn't a valid music file. (I did try both with and w/o the headers.) Maybe it uses a different character set I can't quite put my finger on what might be happening. What I can say is that the yenc coding from the newsgroup article, when viewed with Linux 'more', displays roughly 10% of its characters as a question mark, whereas when I give Python's yenc.encode a binary music file and view its output using 'more', it displays about 90% of the output as a question mark. Any ideas -- John Savage (my news address is not valid for email)
VIEWS ON THIS POST

75

Posted on:

Saturday 3rd November 2012
View Replies!

def X(l=[]): weirdness. Python bug ?

Hi all. I've stumbled onto a python behavior that I don't understand at all. Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) # function def X(l=[]): l.append(1) print l # first call of X X() [1] #second call of X X() [1, 1] Where does the list parameter 'l' live between the two successive calls of X(). Why is it not recreated with an empty list Is this correct behavior or is it a Python bug Does anyone have any pointers to the language documentation where this behavior is described
VIEWS ON THIS POST

72

Posted on:

Sunday 4th November 2012
View Replies!

atan2 weirdness

hi atan2 is supposed to return the angle to x-axis when given y and x, I suppose if I take [x,y] to one full circle, I should get 0-360 degree back---- but no, I get 3 full revolutions! maybe my understanding is wrong. from math import * def f(ang): a=ang if a>360: a-=360 if a>360: a-=360 if a
VIEWS ON THIS POST

84

Posted on:

Sunday 4th November 2012
View Replies!

xml.dom.minidom weirdness: bug?

Hi. I was writing an xmltv parser using python when I faced some weirdness that I couldn't explain. What I'm doing, is read an xml file, create another dom object and copy the element from one to the other. At no time do I ever modify the original dom object, yet it gets modified. Unless I missed something, it sounds like a bug to me. the xml file is simply: full name which I store under the name test.xmltv Here is the code, I've removed everything that isn't applicable to my description. can't make it any simpler I'm afraid: from xml.dom.minidom import Document import xml.dom.minidom def adjusttimezone(docxml, timezone): doc = Document() # Create the base element tv_xml = doc.createElement("tv") doc.appendChild(tv_xml) #Create the channel list channellist = docxml.getElementsByTagName('channel') for x in channellist: #Copy the original attributes elem = doc.createElement("channel") for y in x.attributes.keys(): name = x.attributes[y].name value = x.attributes[y].value elem.setAttribute(name,value) for y in x.getElementsByTagName('display-name'): elem.appendChild(y) tv_xml.appendChild(elem) return doc if __name__ == '__main__': handle = open('test.xmltv','r') docxml = xml.dom.minidom.parse(handle) print 'step1' print docxml.toprettyxml(indent=" ",encoding="utf-8") doc = adjusttimezone(docxml, 1000) print 'step2' print docxml.toprettyxml(indent=" ",encoding="utf-8") Now at "step 1" I will display the content of the dom object, quite natually it shows: full name After a call to adjusttimezone, "step 2" however will show: That's it ! You'll note that at no time do I modify the content of docxml, yet it gets modified. The weirdness disappear if I change the line channellist = docxml.getElementsByTagName('channel') to channellist = copy.deepcopy(docxml.getElementsByTagName('channel')) However, my understanding is that it shouldn't be necessary. Any thoughts on this weirdness
VIEWS ON THIS POST

150

Posted on:

Sunday 4th November 2012
View Replies!

PyOpenGL, wxPython weird behaviour

Hi , I'm doing a project using wxPython and pyopengl, and I seem to have a problem rendering textures. This is code that worked before my hard drive had a meltdown, but not since I re-installed everything. I've determined the problem is in the OpenGL part of my program. I do some calculations to generate a 2D numpy array that holds the image data, and pylab.imshow() shows me the image as it is meant to be. I used the same algorithm in Octave and MATLAB, and all are giving me the right picture. However, using pyOpenGL and the numpyhandler functions (http://cours- info.iut-bm.univ-fcomte.fr/docs/python/OpenGL/ OpenGL.arrays.numpymodule.NumpyHandler-class.html) doesn't seem to work. I get a garbled screen pocked with black pixels. I am including my openGL code below. What am I doing wrong And yes, I did make the dtype of my array 'float32'. -------code snippets------ import wx from wx.glcanvas import GLCanvas from OpenGL.GLU import * from OpenGL.GL import * from OpenGL.arrays.numpymodule import NumpyHandler PC = 1 RI = 0 class myGLCanvas(GLCanvas): def __init__(self, parent): GLCanvas.__init__(self, parent,-1) wx.EVT_PAINT(self, self.OnPaint) self.init = 0 self.mode = -1 # making a texture for the range image self.texture = glGenTextures(1) # making a spot for the point cloud points self.cloud = None return def OnPaint(self,event): dc = wx.PaintDC(self) self.SetCurrent() if not self.init: self.InitGL() self.init = 1 self.OnDraw() return def OnDraw(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) if self.mode == RI: self.drawRange() elif self.mode == PC: self.drawCloud() def InitGL(self): glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(1.0) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL) glClear(GL_COLOR_BUFFER_BIT) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glPixelStorei(GL_PACK_ALIGNMENT, 1) #NTSC colour scales... glPixelTransferf(GL_RED_SCALE, 0.299); glPixelTransferf(GL_GREEN_SCALE, 0.587); glPixelTransferf(GL_BLUE_SCALE, 0.114); glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0.0,1.0,0,1.0,-1.0,1.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() return def rangeImage(self, image): glBindTexture(GL_TEXTURE_2D, self.texture) glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # flatten it into a list so the OpenGL calls work n = NumpyHandler() fI = image.flatten() flatImage = n.dataPointer(n.contiguous(fI)) print n.contiguous(fI) gluBuild2DMipmaps(GL_TEXTURE_2D, 1, image.shape[0]+1, image.shape[1]+1, GL_LUMINANCE, GL_FLOAT, flatImage) self.mode = RI self.OnDraw() def drawRange(self): ''' Controls the actual drawing of the range image''' glMatrixMode(GL_MODELVIEW) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glColor3f(1.0,1.0,1.0) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texture) glBegin(GL_TRIANGLE_FAN) glTexCoord2d(1,1); glVertex3f(0.0, 0.0, 0.0) glTexCoord2d(1,0); glVertex3f(0.0, 1.0, 0.0) glTexCoord2d(0,0); glVertex3f(1.0, 1.0, 0.0) glTexCoord2d(0,1); glVertex3f(1.0, 0.0, 0.0) glEnd() self.SwapBuffers() --------end snippet-----------
VIEWS ON THIS POST

91

Posted on:

Monday 5th November 2012
View Replies!

urllib2 weirdness when https_proxy environment variable is exported

Hi Everyone, I have been extensively using Python's urllib2 while developing a project with the Google Data API. The Google Data API uses httplib to place all of its requests. However I have been using urllib2 and some handlers that I discovered in an ASPN article to handle HTTPS proxies in my code. The Google Data API relies on an environment variable called https_proxy to get information about the proxy to be used. However urllib2 starts spitting out the BadStatusLine exception if the https_proxy environment variable is found. Has anyone experienced similar things with urllib2 Is this a bug in the urllib2 libraries or am I completely missing something here Attached is a dump of the error messages. Any experiences/information/ finding are welcome.
VIEWS ON THIS POST

133

Posted on:

Monday 5th November 2012
View Replies!

cStringIO unicode weirdness

Python 2.5 (r25:51908, Oct 6 2006, 15:24:43) [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import StringIO, cStringIO >>> StringIO.StringIO('a').getvalue() 'a' >>> cStringIO.StringIO('a').getvalue() 'a' >>> StringIO.StringIO(u'a').getvalue() u'a' >>> cStringIO.StringIO(u'a').getvalue() 'a\x00\x00\x00' >>> I would have thought StringIO and cStringIO would return the same result for this ascii-encodeable string. Worse: >>> StringIO.StringIO(u'a').getvalue().encode('utf-8').decode('utf-8') u'a' does the right thing, but >>> cStringIO.StringIO(u'a').getvalue().encode('utf-8').decode('utf-8') u'a\x00\x00\x00' looks bogus. Am I misunderstanding something
VIEWS ON THIS POST

165

Posted on:

Monday 5th November 2012
View Replies!

A bit weird dictionary behavior

, just noticed this: Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> {1: 2} {1: 2} >>> {True: False} {True: False} >>> {1: 2, True: False} {1: False} This must be because >>> True == 1 and True in {1: 2} True but it still doesn't feel exactly right. Would it be worth submitting a bug Cheers, .peke
VIEWS ON THIS POST

94

Posted on:

Wednesday 7th November 2012
View Replies!

Profiling weirdness: Timer.timeit(), fibonacci and memoization

I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1 return b @Decorators.memoize def fibmem(nbr): if nbr > 1: return fibmem(nbr-1) + fibmem(nbr-2) if nbr == 1: return 1 if nbr == 0: return 0 s = 100 t1 = Timer('fib(s)', 'from __main__ import fib, s') t2 = Timer('fibmem(s)', 'from __main__ import fibmem, s') t1.repeat(number=1) t2.repeat(number=1) print t1.timeit() print t2.timeit() >>> 35.3092010297 1.6516613145 >>> So memoization is 20+ times faster than the idiomatic way Or am I missing something here Ok for fun I added memoization to the idiomatic one: from timeit import Timer import Decorators @Decorators.memoize def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1 return b @Decorators.memoize def fibmem(nbr): if nbr > 1: return fibmem(nbr-1) + fibmem(nbr-2) if nbr == 1: return 1 if nbr == 0: return 0 s = 100 t1 = Timer('fib(s)', 'from __main__ import fib, s') t2 = Timer('fibmem(s)', 'from __main__ import fibmem, s') t1.repeat(number=1) t2.repeat(number=1) print t1.timeit() print t2.timeit() didn't think it would make a difference there but it certainly did. >>> 1.59592657726 1.60179436213 >>> ================================ RESTART ================================ >>> 2.66468922726 3.0870236301 >>> ================================ RESTART ================================ >>> 1.62921684181 1.51585159566 >>> ================================ RESTART ================================ >>> 1.49457319296 1.60948472501 >>> ================================ RESTART ================================ >>> 1.48718203012 1.6645559701 >>>
VIEWS ON THIS POST

74

Posted on:

Wednesday 7th November 2012
View Replies!

xml.dom's weirdness?

Why this generates AttributeError, then not Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import xml >>> xml.dom Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'dom' >>> xml.dom >>> , Lie wrote: > Question: Is there a way to list loaded modules, including those that > aren't in my namespace such as sys.modules Modules are not unloaded automatically just because you do not use them yourselves. If the module is imported for whatever reason by whatever other module, it stays alive until it's no longer referenced (including the reference in sys.modules). Stefan , On 28 Jul, 16:15, Sion Arrowsmith wrote: > Stefan Behnel wrote: > >Blame Ubuntu/Debian. > > I'd be wary about including Debian in that blame. Using 4.0 here, > with a 2.4.4 default and a 2.5.0 straight from the package: [...] I can imagine that some of the Python Eggs magic could quite easily inflate the number of files opened when doing imports. Information on things like the state of site-packages, .pth files, the length of PYTHONPATH and so on should surely accompany the numbers before fingers are pointed. Paul
VIEWS ON THIS POST

88

Posted on:

Wednesday 7th November 2012
View Replies!

Very weird bug!

I was looking into currying and Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 1.2.2 >>> h = "aja baja" >>> h += 'e' >>> h 'aja bajae' >>> h = h+'a' >>> h 'aja bajaea' >>> a = b = "foo" >>> a 'foo' >>> b 'foo' >>> a==b True >>> id(a) 35018400 >>> id(b) 35018400 >>> a += "bar" >>> id(a),a (35110112, 'foobar') >>> id(b),b (35018400, 'foo') >>> fac Traceback (most recent call last): File "", line 1, in fac NameError: name 'fac' is not defined >>> factorial Traceback (most recent call last): File "", line 1, in factorial NameError: name 'factorial' is not defined >>> import math >>> math >>> math.factorial Traceback (most recent call last): File "", line 1, in math.factorial AttributeError: 'module' object has no attribute 'factorial' >>> math.fac Traceback (most recent call last): File "", line 1, in math.fac AttributeError: 'module' object has no attribute 'fac' >>> dir(math) ['__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] >>> pow >>> tan Traceback (most recent call last): File "", line 1, in tan NameError: name 'tan' is not defined >>> math.tan >>> def build(a,b): return a+b >>> build(5,4) (5, 4) >>> def build(x,y): a=x+y return a >>> build(5,4) 9 >>> def b(n, p): return n + p >>> b(1, 10) 11 >>> def b(n,p): return n+p >>> b(5,2) 7 >>> def build(x,y): return a+b >>> build(5,4) Traceback (most recent call last): File "", line 1, in build(5,4) File "", line 2, in build return a+b TypeError: cannot concatenate 'str' and 'function' objects >>> def build(x,y): yreturn x+ SyntaxError: invalid syntax >>> def build(x,y): return x+y >>> build(5,4) 9 >>> def build(a,b): return a+b >>> build(5,4) 9 >>> wtf was this in the middle! >>> def build(a,b): return a+b >>> build(5,4) (5, 4)
VIEWS ON THIS POST

65

Posted on:

Wednesday 7th November 2012
View Replies!

weird iteration/assignment problem

for i in xrange(0, len(texts)): texts[i] = "yes" for i in texts: i = "no" why is the first one working but not the second. i mean i see why the firts one works but i dont udnerstand why the second doesnt.
VIEWS ON THIS POST

57

Posted on:

Wednesday 7th November 2012
View Replies!

Very weird behavior in MySQLdb "execute"

This has me completely mystified. Some SELECT operations performed through MySQLdb produce different results than with the MySQL graphical client. This failed on a Linux server running Python 2.5, and I can reproduce it on a Windows client running Python ...
VIEWS ON THIS POST

68

Posted on:

Sunday 11th November 2012
View Replies!

Tkinter weird (and randomly inconsistant) crashes

Hi , My setup is: WinXP Python 2.5.1 TKinter version: $Revision: 50704 $ Tcl: 8.4 Debugger: WinPdb My program uses some Tkinter code written by someone else, that creates a basic 24x80 terminal across different platforms. The terminal worked fine ...
VIEWS ON THIS POST

49

Posted on:

Monday 12th November 2012
View Replies!

weird embedding problem

Hi all, I'm embedding python in a C prog which is built as a linux shared lib. The prog is linked against libpython, and on startup, it calls Py_Initialize(). The prog imports a pure-python script. The script starts up ok, ...
VIEWS ON THIS POST

90

Posted on:

Monday 19th November 2012
View Replies!

imported module no longer available

I've come across an error that i'm not yet able to create a test case for but wanted to get see if someone could shed light on this. I have imported a module at the top of my file with ...
VIEWS ON THIS POST

58

Posted on:

Sunday 25th November 2012
View Replies!

Mutually referencing imports -- impossible?

I started off with a module that defined a class Vehicle, and then subclasses Car and Motorcycle. In the Car class, for some bizarre reason, I instantiated a Motorcycle. Please pretend that this can't be avoided for now. Meanwhile, my ...
VIEWS ON THIS POST

101

Posted on:

Sunday 25th November 2012
View Replies!

Re: Function to import module to namespace

Terry Reedy wrote: > > > Do you mean something like this > >>> math.__dict__.update(string.__dict__) > >>> dir(math) > ['Formatter', 'Template', '_TemplateMetaclass', '__builtins__', I think this is working.... First off, 2 module files: funcs.py def func1(): print ...
VIEWS ON THIS POST

60

Posted on:

Sunday 25th November 2012
View Replies!

Threads and import

I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with another lump of code). The ...
VIEWS ON THIS POST

62

Posted on:

Sunday 25th November 2012
View Replies!