[Tutor] Troubles with Python modules

Alberto Troiano albertito_g at hotmail.com
Tue May 17 21:22:46 CEST 2005


First of all, thanks for the corrections
I'm assuming that Python 2.2 doesn't have datetime module then???????
Is there any way to install it???????

My version of Python 2.3.4 is an alternative installation so I know that 
when I type python with my root account its going to Python 2.2.2 (I'm using 
the root account) I changed the link though so when I say Python I enter 
Python 2.3.4 (works and checked)

I'm sending the errors MySQLdb gives me when I try to install it. It's 
hola.txt file
I'm doing python setup.py build and inside hola.txt you can see what it says
I have installed Linux Red Hat with ALL packages

Can I add datetime module to Python 2.2.2 ??????
If so how???????

Regards

Alberto

>From: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
>To: Alberto Troiano <albertito_g at hotmail.com>
>CC: Tutor <tutor at python.org>
>Subject: Re: [Tutor] Troubles with Python modules
>Date: Tue, 17 May 2005 09:58:10 -0700 (PDT)
>
>
>
>On Tue, 17 May 2005, Alberto Troiano wrote:
>
> > I'm working on Python 2.2 over Linux REd Hat 9.0 and here is the code I
> > have
>
>[code cut]
>
> > First I'd like to know if this code can be shorter or more efficient (if
> > you have the time)
>
>Yes.  But let's look at the errors first.
>
>
>
> > Second the error is the following:
> >
> > Traceback (most recent call last):
> >   File "/root/cotascamon.py", line 4, in ?
> >     import datetime
> > ImportError: No module named datetime
> >
> > I think this module is in Python 2.3. What can I do??????
>
>I'll assume for the moment that you are using your root account.  Are you
>sure that your root account is using Python 2.3?  It may be possible that
>the root account has a slightly more restricive PATH than normal user
>accounts, and that you might be picking up '/usr/bin/python'.
>
>
>
> > Then I have installed Python 2.3.4 in the same Linux but I can't use it
> > because it doesn't recognze the module MySQLdb
> >
> > Here is the error
> >
> > Traceback (most recent call last):
> >   File "/root/cotascamon.py", line 3, in ?
> >     import MySQLdb
> >   File "/usr/local/lib/python2.3/site-packages/MySQLdb/__init__.py", 
>line
> > 27, in ?
> >     import _mysql
> > ImportError: No module named _mysql
>
>
>Different releases of Python will not automatically migrate the old
>third-party modules.  You'll need to reinstall MySQLdb for Python 2.3.
>MySQLdb can be found here:
>
>     http://sourceforge.net/projects/mysql-python
>
>
>
>
>Let's look at some of the code.
>
>
> >     def consulta(self,query,args=None):
> >         try:
> >             self.cursor=self.db.cursor()
> >             self.sql=self.cursor.execute(query,args)
> >         except:
> >             self.cerrar()
> >         else:
> >             self.cerrar()
>
>The consulta() function tries to make sure that the cerrar()  method is
>called, no matter what.  In this case, try/finally may do what you want:
>
>######
>try:
>     ...
>finally:
>     self.cerrar()
>######
>
>
>
> > def getpath():
> >     global ruta
> >     global user
> >     global cam
> >     try:
> >         i=0
> >         for arg in sys.argv:
> >             if i==1:
> >                 ruta+=arg
> >             elif i==2:
> >                 user=arg
> >             elif i==3:
> >                 cam=arg
> >             else:
> >                 pass
> >             i+=1
> >     except:
> >         f=open("cotascamonerrors.log","a+")
> >         f.write("ERROR ------ No se pudo encontrar el path "+ruta+".
> > Contacte al administrador.")
> >         f.close()
>
>This looks like it's trying to do argument parsing, storing state in
>global variables.  You may want to see if you can avoid the globals, and
>instead just return those three values back to the caller.
>
>My nervousness with the globals comes from the requirement that getpath()
>has to assume that 'ruta' already has some sort of value already.  So
>there's already a dependency that can be more clearly documented by making
>getpath() take in an initial base path argument:
>
>######
>def getpath(base_path):
>     ...
>     return (ruta, user, cam)
>######
>
>
>But the block above also feels a little awkward because it assumes that
>any exception that occurs has to be a path problem.  That might not
>necessarily be the case.  I'd strongly recommend letting the exception
>speak for itself.  traceback.print_exc() can help:
>
>     http://www.python.org/doc/lib/module-traceback.html
>
>######
>try:
>     ....
>except:
>     f = open("cotascamonerrors.log", "a+")
>     traceback.print_exc(file=f)
>     f.close()
>######
>
>
>Up to this point, though, things are pretty ok, with the nice helper
>functions with clear roles.  The large block near the bottom, though,
>needs work.  I'd recommend applying the same helper-function breakup to
>make the code's intent clearer.
>
>
>For example, here's SQL code that's repeated, a.  It should be broken out:
>
> >             try:
> >                 os.system("cp webcam.jpg grabacion/cam"+grupo+".jpg")
> >                 query="INSERT INTO imagen values 
>(%s,%s,%s,NOW(),NOW(),%s)
> > where CodigoCamara=%s and Usuario=%s"
> >                 grupo1=datetime.datetime.today().strftime("%Y%m%d%H%M")
> >                 ruta1=ruta+"/grabacion/cam"+grupo+".jpg"
> >                 args(user,cam,ruta1,grupo1)
> >                 cur.consulta(query,args)
> >             except:
> >                 pass
>
>
>Again, try not to obscure exceptions.  If something bad happens, you
>really want the exception to tell you what happened: it is not fun to
>debug something when the error message is insufficiently helpful.  Change
>the except block to log the error message.
>
>
>
>I do not understand what's happening with args() above:
>
> >                 args(user,cam,ruta1,grupo1)
>
>Do you mean:
>
>                   args = (user,cam,ruta1,grupo1)
>
>instead?
>
>
>Hope this helps!
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor


Gaucho

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: hola.txt
Url: http://mail.python.org/pipermail/tutor/attachments/20050517/dbac7393/hola-0001.txt


More information about the Tutor mailing list