installation of pymongo works but fails at import in python3

孤者浪人 提交于 2019-12-13 12:28:07

问题


I'm currently running Ubuntu 12.10 and tried to get pymongo to work properly under python3. Things I have tried:

1. apt-get install python-pymongo
2. python setup.py install #from git source
3. easy_install pymongo
4. easy_install pymongo3
5. pip install pymongo
6. pip install pymongo3 #needed a fix in the download script

I have also removed and cleaned between the installations as best as I could.

If I import pymongo:

In [1]: import pymongo
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-ec8fdd1cd630> in <module>()
----> 1 import pymongo

/usr/local/lib/python3.2/dist-packages/pymongo-2.4.2_-py3.2-linux-x86_64.egg/pymongo/__init__.py in <module>()
     55     return '.'.join(map(str, version_tuple))
     56 
---> 57 version = get_version_string()
     58 """Current version of PyMongo."""
     59 

/usr/local/lib/python3.2/dist-packages/pymongo-2.4.2_-py3.2-linux-x86_64.egg/pymongo/__init__.py in get_version_string()
     51 
     52 def get_version_string():
---> 53     if isinstance(version_tuple[-1], basestring):
     54         return '.'.join(map(str, version_tuple[:-1])) + version_tuple[-1]
     55     return '.'.join(map(str, version_tuple))

NameError: global name 'basestring' is not defined

The error is the same as pymongo FAQ but for me it doesn't matter where I'm located.

My theory is that the python2 version of pymongo has mistakenly been added python3.


回答1:


In case I needed it in the future.

Installation of pymongo in ubuntu13 for python3

(1)Do not install the pymongo directly in the ubuntu software center; they are for old version of python (before version 3)

If you install it, you'll not be able to use pymongo in python3:

(2)You should install a specific python package installer for python 3: python3-pip

(3)use the pip-3.3 command python3-pip package provided, run the following command in terminal of unbuntu to install pymongo:

pip-3.3 install pymongo

If you don't have super user privilege, run it with sudo

sudo pip-3.3 install pymongo

(4)Then you should have no difficulty in install pymongo which can be used in python3. To verify it, type in IDLE:

(5)You can also install pymongo3 without difficulty in the terminal:

pip-3.3 install pymongo3

But there is no need to install it because all its functionalities are included in current pymongo package now.




回答2:


Installation with pip3 worked for me

sudo pip3 install pymongo

To use pip3, As a prerequisite Python 3 copy of pip has to be installed which can be done as follows

sudo apt-get install python3-pip



回答3:


  1. Download the latest pymongo(3.4.0) from https://pypi.python.org/pypi/pymongo#downloads

  2. Extract the files: tar -zxvf pymongo-3.4.0.tar.gz

  3. Run the installation by firing: python3 setup.py install

Note: Of course you will need to fire the aforementioned command inside your extracted pymongo directory. You may also need to run it as root otherwise you will get write permission issues. I am using CentOS 7 and it worked like breeze!.




回答4:


It should work with

sudo python3.2 setup.py install

but it doesn't for some reason

better of downloading the source for distributed and pip (and install them in this order) by

sudo python3 setup.py install #distributed
sudo python3 setup.py install #pip
sudo pip-3.2 install pymongo


来源:https://stackoverflow.com/questions/14879527/installation-of-pymongo-works-but-fails-at-import-in-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!