我正在尝试使用
mongodb和pymongo运行Web应用程序来提供数据库中的数据.
我得到的错误是ImportError:没有名为parse的模块.请参阅以下来自apache2 web服务器的error.log:
mod_wsgi (pid=18824): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[:error] [pid 18824:tid 139967053518592] mod_wsgi (pid=18824): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[:error] [pid 18824:tid 139967053518592] Traceback (most recent call last):
File "/var/www/FlaskApp/flaskapp.wsgi", line 12, in <module>
[:error] [pid 18824:tid 139967053518592] from ABC import app as application
[:error] [pid 18824:tid 139967053518592] File "var/www/FlaskApp/ABC/__init__.py", line 1, in <module>
[:error] [pid 18824:tid 139967053518592] from pymongo import MongoClient
[:error] [pid 18824:tid 139967053518592] File "/var/www/FlaskApp/ABC/venv/lib/python3.4/site-packages/pymongo/__init__.py", line 92, in <module>
[:error] [pid 18824:tid 139967053518592] from pymongo.connection import Connection
[:error] [pid 18824:tid 139967053518592] File "/var/www/FlaskApp/ABC/venv/lib/python3.4/site-packages/pymongo/connection.py", line 39, in <module>
[:error] [pid 18824:tid 139967053518592] from pymongo.mongo_client import MongoClient
[:error] [pid 18824:tid 139967053518592] File "/var/www/FlaskApp/ABC/venv/lib/python3.4/site-packages/pymongo/mongo_client.py", line 46, in <module>
[:error] [pid 18824:tid 139967053518592] from pymongo import (auth,
[:error] [pid 18824:tid 139967053518592] File "/var/www/FlaskApp/ABC/venv/lib/python3.4/site-packages/pymongo/uri_parser.py", line 18, in <module>
[:error] [pid 18824:tid 139967053518592] from urllib.parse import unquote_plus
[:error] [pid 18824:tid 139967053518592] ImportError: No module named parse
我有Python 3.4,Flask和pymongo的虚拟环境.我使用的是mongodb 2.6.7.
是什么原因导致问题?
最佳答案 看起来您正在使用python 2.x运行您的应用程序,但您的应用程序使用的模块来自python 3.x.特别是,pymongo正在尝试导入模块urllib.parse,它在python 2.x中被称为urlparse.因此,使用python 2.x执行import urllib.parse会导致ImportError.
I have virtual environment for Python 3.4,
你是如何激活虚拟环境的?
回复评论:
I think I did not use virtual environment to install Mongodb
没关系.虚拟环境中的pymongo代码是连接到您的mongodb服务器的(使用指定的端口).
After installation of Flask and pymongo I deactivated virtual environment.
您是否按照Flask docs中关于mod_wsgi,virtualenv和设置activate_this变量的说明进行操作?
对评论#2的回应:
My web-site works in a static mode, only when I start using database
it stops working due to this problem with parse module
是的,您的网站在使用python 2.x执行时工作正常,但是当您开始使用数据库时,您正在使用尝试在python 3.x中导入库的模块.因此,如果您继续使用python 2.x来执行您的站点,那么您将不会使用数据库.
I could not figure out what I need to put inside activate_this.py.
试试这个:
1)转到包含虚拟环境的目录:
$cd /some/path/to/venv
2)列出所有文件:
$ls
3)切换到bin目录:
$cd bin
4)列出所有文件:
$ls
5)打开文件activate_this.py并阅读顶部的注释,例如
$vi activate_this.py
6)点击我在之前的回复中发布的Flash文档的链接,然后再次阅读那里的材料.