-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Knowing that mongoengine is python 3 compatible, I naturally built a python 3.x virtual env to start contributing and installed mongoengine in editable mode (using 'pip install -e'). And there started my struggles...
When running python code in my python venv (through an ipython shell for instance), things were failing weirdly. But I noticed that it was only failing when opening my shell from the mongoengine root's directory... In fact, here's how it was failing:
import mongoengine as me
me.connect()
class Person(me.Document):
name = me.StringField()
person = Person() # raises AttributeError: 'Person' object has no attribute '_is_document'
Person.objects() # raises AttributeError: type object 'Person' has no attribute 'objects'
It was failing due to the metaclass that weren't loaded?!
So I started digging in the code and noticed that mongoengine's codebase was entirely written in python2 and the compatibility of the library under python 3 relied on using 'use_2to3' parameter in setup.py. I didn't know about this. So, when running 'pip install -e', this flag makes pip first run 2to3 on the codebase and copy the new code in mongoengine/build, and only after that the python3 version of the code is installed in the virtual env. This means that depending on how I was running the code above, I was sometime using the python2 file (mongoengine/document.py) and sometime the python3 file (from mongoengine/build/mongoengine/documents.py) from my python 3 interpreter...
When I look back at it, it sounds obvious but I've found 2 other related issues: #1819 , #1674 so I believe that it could be better documented and/or reported. The best fix will be to make the code 2/3 compatible (and that is documented in the wiki https://github.com/MongoEngine/mongoengine/wiki/Towards-1.0) but in the meantime I suggest to do the following:
- Document this clearly in the readme and/or contributing files
- Identify this in the code and raise a particular exception (I still need to find a clean/clever way to do this)
I'll send a PR shortly but please let me know if you have other thoughts