Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named parse

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?

like image 372
user3151858 Avatar asked Jan 31 '15 01:01

user3151858


People also ask

What does Urllib parse Urlencode do?

parse. urlencode() method can be used for generating the query string of a URL or data for a POST request.

How do you use Urlparse in Python?

There are several ways to assemble a split URL back together into a single string. The parsed URL object has a geturl() method. geturl() only works on the object returned by urlparse() or urlsplit(). If you have a regular tuple of values, you can use urlunparse() to combine them into a URL.


2 Answers

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.

like image 186
7stud Avatar answered Sep 28 '22 02:09

7stud


Python2.7:

from urlparse import urlparse

Python3:

from urllib.parse import urlparse

python2 urlparse

like image 43
Nancy Avatar answered Sep 28 '22 01:09

Nancy