Can not import copy_reg

1,763 views
Skip to first unread message

Richard

unread,
Jul 22, 2014, 7:05:06 AM7/22/14
to web...@googlegroups.com
Hi,

I have an issue with adjusting the auth tables resulting in a custom import and an error that copy_reg can not be imported.

The error trace is:
Traceback (most recent call last):
File "/home4/mamplcom/public_html/cgi-bin/gluon/restricted.py", line 220, in restricted
exec ccode in environment
File "/home4/mamplcom/public_html/cgi-bin/applications/im/models/db.py", line 74, in <module>
auth.define_tables(username=False, signature=False)
File "/home4/mamplcom/public_html/cgi-bin/gluon/tools.py", line 1784, in define_tables
format='%(first_name)s %(last_name)s (%(id)s)'))
File "/home4/mamplcom/public_html/cgi-bin/gluon/dal.py", line 8226, in define_table
table = self.lazy_define_table(tablename,*fields,**args)
File "/home4/mamplcom/public_html/cgi-bin/gluon/dal.py", line 8263, in lazy_define_table
polymodel=polymodel)
File "/home4/mamplcom/public_html/cgi-bin/gluon/dal.py", line 1125, in create_table
sql_fields_old = pickle.load(tfile)
File "/home4/mamplcom/public_html/cgi-bin/gluon/custom_import.py", line 92, in custom_importer
return base_importer(pname, globals, locals, fromlist, level)
ImportError: No module named copy_reg
I can import copy_reg from the python prompt and adding the import copy reg from Dal.py (suggested in a thread by Massimo) has no effect.
I use Python 2.7.8.
Any clu would be helpfull.
Richard

Massimo Di Pierro

unread,
Jul 22, 2014, 7:21:36 AM7/22/14
to web...@googlegroups.com
Copy reg is a python module. Can you import it from the normal python shell?

Richard

unread,
Jul 22, 2014, 2:04:18 PM7/22/14
to web...@googlegroups.com
Yes, I can import copy_reg from the Python prompt on the remote server.

Massimo Di Pierro

unread,
Jul 23, 2014, 3:49:37 AM7/23/14
to web...@googlegroups.com
Can you try from the web2py shell?

python web2py.py -S welcome -P

Richard

unread,
Jul 23, 2014, 5:03:48 AM7/23/14
to web...@googlegroups.com
When I add the import copy_reg in db.py I get the error:

<type 'exceptions.ImportError'> No module named copy_reg


In the python/Python-2.7.8/Lib directory are 3 copy_reg files:
- copy_reg.py
- copy_reg.pyc
- copy_reg.pyo

Maybe of any relevance: I run a shared Apache server with the following first line in cgihandler.py :  #!/home4/mamplcom/python/Python-2.7.8/python



On Tuesday, July 22, 2014 1:21:36 PM UTC+2, Massimo Di Pierro wrote:

Massimo Di Pierro

unread,
Jul 23, 2014, 5:20:06 AM7/23/14
to web...@googlegroups.com
I understand but can you open a web2py shell. from the console type:


$ cd web2py
python web2py.py -S welcome -P
>>> import copy_reg

Richard

unread,
Jul 23, 2014, 5:40:14 AM7/23/14
to web...@googlegroups.com
Massimo,

importing copy_reg from the console works fine :)

Richard

Niphlod

unread,
Jul 23, 2014, 6:30:11 AM7/23/14
to web...@googlegroups.com
I'd vote for the usual issue with apache and mod_wsgi not compiled against the latest interpreter....

Richard

unread,
Jul 23, 2014, 7:07:38 AM7/23/14
to web...@googlegroups.com
The issue does not occur on my local development machine. 
I did an upgrade on the remote host where the issue occurs using the web2py update feature. Then I upgraded from Python 2.6 to 2.7.8. All apps works fine but not the one where I add features to auth.

How should resolve this in a manner that I have a solid basis for production? We are in a pre-production phase.

Niphlod

unread,
Jul 23, 2014, 3:45:45 PM7/23/14
to web...@googlegroups.com
I seem to be the only "advocate" for ditching apache always and before anything else (especially before going into production, where a not-so-well-tuned apache (most-likely you're using everybody's else defaults) still suffers a Slowloris attack).
Those kind of headaches are non-existent with other solutions (often more performant without particular tuning).
Check with a simple google search for "mod_wsgi recompile" to see how many people have been in your situation (mismatch between what mod_wsgi has been originally compiled vs the current running python version) and their solutions for various distributions. It's a pretty well documented "issue" with a large number of cases.

Richard

unread,
Jul 23, 2014, 4:00:36 PM7/23/14
to web...@googlegroups.com
Thank you for the answer, I did hope for an other answer but i'll go in the Google mode.
i'll keep you informed about the progress.

Massimo Di Pierro

unread,
Jul 24, 2014, 3:21:24 AM7/24/14
to web...@googlegroups.com
I agree with you that Apache is not a good solution any more. Nginx for example is a much better solution. Nevertheless some people may not have a choice.

Richard

unread,
Jul 24, 2014, 3:15:12 PM7/24/14
to web...@googlegroups.com
I am hosted at JustHost, who has the option on installing Nginx. I'll try that for a better infrastructure.

Richard

unread,
Jul 25, 2014, 7:32:28 AM7/25/14
to web...@googlegroups.com
nginx will need a VPS so i'll stick to CGI for prototyping and buy some time for a new hosting service. 
In the book an option for "Some web hosting services do not support mod_wsgi. In this case, we must use Apache as a proxy and forward all incoming requests to the web2py built-in web server (running for example on localhost:8000)."

I'll try this first.

Richard

unread,
Jul 26, 2014, 2:12:42 PM7/26/14
to
While setting up a new server I am wondering why previous versions of my app worked fine.
What functionality triggers the custom_import trying to import copy_reg? A temporarily solution might miss this functionality and I'll build this back when the hosting is in place again.

Richard

unread,
Jul 30, 2014, 2:18:02 AM7/30/14
to web...@googlegroups.com
Some progress is not what I expect :( . It takes time to learn how to configure a webserver.

I am now hosting the app at Pythonanyware, I expect the libraries they use are up to date. assuming they are, also at pythonanyware running python2.7 the same error occurs.

Could it be a bug in Web2py? Please assist me in diagnosing and testing so nobody else will run in this issue.

Richard

Giles Thomas

unread,
Jul 30, 2014, 12:42:06 PM7/30/14
to web...@googlegroups.com
Hi all,

Giles from PythonAnywhere here.  I've been working with Richard to try to track this down further.  One thing I can confidently say is that it's not an Apache/mod_wsgi problem in this case, because we use nginx and uWSGI.

I've also double-checked that the `copy_reg` import works from a trivial Flask app, and that was fine, so I don't think it's the interpreter.

Does anyone have any thoughts on how we could further debug this?


All the best,

Giles

Leonel Câmara

unread,
Jul 30, 2014, 1:12:47 PM7/30/14
to
My guess would be this is happening because of you copying files with pickles around. Some stuff is not portable, for instance, web2py uses marshal in some places.

Pickle is infamous for giving very weird error messages like the one you are getting that appear to point to other problems.

If I am right, a solution can be to delete everything with pickles on it. Like the table files in the database folder, the sessions, and everything in the cache.

Giles Thomas

unread,
Jul 30, 2014, 1:33:18 PM7/30/14
to web...@googlegroups.com
Thanks, Leonel.  We'll look into that.


On 30 July 2014 18:12, Leonel Câmara <leonel...@gmail.com> wrote:
My guess would be this is happening because of you copying files with pickles around. Some stuff is not portable, for instance, web2py uses marshal in some places.

Pickle is infamous for giving very weird error messages like the one you are getting that appear to point to other problems.

If I am right, a solution can be to delete everything with pickles on it. Like the table files in the database folder, the sessions and everything in the cache.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/8toVDprfqwM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Richard

unread,
Jul 30, 2014, 3:36:33 PM7/30/14
to
Progress made :) On PythonAnywhere I can clean cache, sessions and database and start storing data again. But packing the app importing on my development machine, editing and packing and restoring on PA gives the same error. Some thing on my machine is wrong I'll find out! :)
Happily it is no bug in Web2py.

Leonel, Giles, thank you for the support !

Richard 

Leonel Câmara

unread,
Jul 30, 2014, 4:48:52 PM7/30/14
to web...@googlegroups.com
Richard if packing and unpacking the application in pythonanywhere is causing problems the cause may be because .pyc files are going with your application pack (you can open the application pack with any archive application and check), maybe in your modules folder. If that's the case this could probably be considered a web2py bug.

Niphlod

unread,
Jul 30, 2014, 5:00:16 PM7/30/14
to web...@googlegroups.com
well, you can't expect a compiled app to work amongst python versions..... you should compile it on the destination ....

Leonel Câmara

unread,
Jul 31, 2014, 5:00:45 AM7/31/14
to web...@googlegroups.com
I agree niphlod, what I am saying may be a bug is that admin should probably make sure pyc files do not go anywhere in the application pack by default.

Niphlod

unread,
Jul 31, 2014, 6:02:54 AM7/31/14
to web...@googlegroups.com
uhm.....I don't think anybody put a triple check for it.... but you may want to ship a compiled app without the sourcecode available for your customers, e.g. to update a fixed release.

Leonel Câmara

unread,
Jul 31, 2014, 6:26:48 AM7/31/14
to web...@googlegroups.com
Frankly, I think it's borderline retarded to distribute python applications without the source code. Firstly, pyc files are not portable, secondly pyc files are trivial to reverse using something like https://github.com/Mysterie/uncompyle2, and thirdly, your app may actually stop working if your client upgrades Python unless you're also bundling your own Python interpreter.  
  
Basically, if you don't want people to have access to your source code, you shouldn't be using Python, or, if you really don't want to give the source to the clients, you should be forcing your clients to use your own hosting that you control.
  
For these reasons, I don't think distributing as pycs should even be an option.

Richard

unread,
Jul 31, 2014, 10:40:34 AM7/31/14
to web...@googlegroups.com
My current situation is that I can move the software with no sqlite database to PA, fill the database, looks ok, not efficient though.
Packing the app with no errors, no chache, but sqlite file, gives the import error.
Zip instead of pack has the same result.

But distribution using zip/unzip without the database gives no error. In fact this will be the case in most cases in my current situation.

Maybe something is wring in adding extra fields to auth. I do the following:

auth.settings.extra_fields['auth_user']= [ 
    Field('community', 'reference community', IS_IN_DB(db,'community.id', '%(com_name)s (%(id)s)'), label=T('Community')),
    Field('langu', 'reference langu', requires=IS_IN_DB(db,'langu.id','%(langu)s'))]

auth.settings.extra_fields['auth_group']= [
    Field('community', 'reference community', label=T('Community'),
    requires=IS_EMPTY_OR(IS_IN_DB(db,'community.id','%(com_name)s (%(id)s)'))) ]

auth.define_tables(username=False, signature=False)

Where this last line for the auth table creation raises the can not import copy_reg error.

Leonel Câmara

unread,
Jul 31, 2014, 11:21:42 AM7/31/14
to
Richard basically it's the .table files in your database folder that are creating the problem. You can set migrate=False in your DAL call on db.py  before packing the application to prevent the problem, then turn it back on in your development machine. 
  
Table files is where web2py stores your table's structure to detect changes and do migrations, since the structure of the database is already going to be correct as any migration has already been done in your development machine there's no need to do it in the server and have these problems.

Richard

unread,
Aug 1, 2014, 4:20:27 PM8/1/14
to web...@googlegroups.com
Leonel, switching migrate to False works indeed perfect in my case. Thank you :)
Richard
Reply all
Reply to author
Forward
0 new messages