Python 导入错误:没有名为 parse 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28247374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: No module named parse
提问by user3151858
I am trying to run web application using mongodb and pymongo to serve data from database.
我正在尝试使用 mongodb 和 pymongo 运行 Web 应用程序来提供数据库中的数据。
The error I am getting is ImportError: No module named parse. Please see below error.log from apache2 web server:
我得到的错误是 ImportError: No module named 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
I have virtual environment for Python 3.4, Flask, and pymongo. I am using mongodb 2.6.7.
我有 Python 3.4、Flask 和 pymongo 的虚拟环境。我正在使用 mongodb 2.6.7。
Any ideas what causes issue?
任何想法是什么导致问题?
采纳答案by 7stud
It looks like you are running your app with python 2.x, but the modules your app uses are from python 3.x. In particular, pymongo
is trying to import the module urllib.parse
, which was called urlparse
in python 2.x. As a result, executing import urllib.parse
with python 2.x causes an ImportError.
看起来您正在使用 python 2.x 运行您的应用程序,但您的应用程序使用的模块来自 python 3.x。特别是,pymongo
正在尝试导入在 python 2.x 中urllib.parse
调用的模块urlparse
。因此,import urllib.parse
使用 python 2.x执行会导致导入错误。
I have virtual environment for Python 3.4,
我有 Python 3.4 的虚拟环境,
How did you activate your virtual environment?
你是如何激活你的虚拟环境的?
Response to comment:
回复评论:
I think I did not use virtual environment to install Mongodb
我想我没有使用虚拟环境安装Mongodb
That's fine. The pymongo code inside your virtual env is what connects to your mongodb server (using a specified port).
没关系。虚拟环境中的 pymongo 代码是连接到 mongodb 服务器的代码(使用指定的端口)。
After installation of Flask and pymongo I deactivated virtual environment.
安装 Flask 和 pymongo 后,我停用了虚拟环境。
Have you followed the instructions in the Flask docswith regards to mod_wsgi
, virtualenv
, and setting the activate_this
variable?
您已按照说明书瓶文档与问候mod_wsgi
,virtualenv
以及设置activate_this
变量?
Response to comment #2:
对评论 #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
我的网站在静态模式下工作,只有当我开始使用数据库时,它才会因为解析模块的这个问题而停止工作
Yes, your site works fine while being executed with python 2.x, but when you start using the db, you are using modules that try to import libraries inside python 3.x. As a result, if you continue to use python 2.x to execute your site, then you are not going to be using a db.
是的,您的站点在使用 python 2.x 执行时运行良好,但是当您开始使用 db 时,您正在使用尝试在 python 3.x 中导入库的模块。因此,如果您继续使用 python 2.x 来执行您的站点,那么您将不会使用 db。
I could not figure out what I need to put inside activate_this.py.
我不知道我需要在 activate_this.py 里面放什么。
Try this:
尝试这个:
1) Go to the directory containing your virtual environment:
1)转到包含您的虚拟环境的目录:
$ cd /some/path/to/venv
2) List all the files:
2)列出所有文件:
$ ls
3) Change into the bin directory:
3)进入bin目录:
$ cd bin
4) List all the files:
4)列出所有文件:
$ ls
5) Open the file activate_this.py
and read the comments at the top, e.g.
5)打开文件activate_this.py
并阅读顶部的评论,例如
$ vi activate_this.py
6) Click on the link to the Flash docs I posted in my previous response and read the material there again.
6) 单击指向我在之前回复中发布的 Flash 文档的链接,然后再次阅读那里的材料。
回答by Nancy
Python2.7:
Python2.7:
from urlparse import urlparse
Python3:
蟒蛇3:
from urllib.parse import urlparse