ImportError: No module named parse

半世苍凉 提交于 2020-01-02 08:55:17

问题


I am trying to run web application using mongodb and pymongo to serve data from database.

The error I am getting is ImportError: No module named parse. Please see below error.log from apache2 web server:

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.

Any ideas what causes issue?


回答1:


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.

I have virtual environment for Python 3.4,

How did you activate your virtual environment?

Response to comment:

I think I did not use virtual environment to install Mongodb

That's fine. The pymongo code inside your virtual env is what connects to your mongodb server (using a specified port).

After installation of Flask and pymongo I deactivated virtual environment.

Have you followed the instructions in the Flask docs with regards to mod_wsgi, virtualenv, and setting the activate_this variable?

Response to comment #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.

I could not figure out what I need to put inside activate_this.py.

Try this:

1) Go to the directory containing your virtual environment:

$ cd /some/path/to/venv

2) List all the files:

$ ls 

3) Change into the bin directory:

$ cd bin

4) List all the files:

$ ls

5) Open the file activate_this.py and read the comments at the top, e.g.

$ 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.




回答2:


Python2.7:

from urlparse import urlparse

Python3:

from urllib.parse import urlparse

python2 urlparse



来源:https://stackoverflow.com/questions/28247374/importerror-no-module-named-parse

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