Satwik Kansal
Satwik Kansal

Reputation: 61

Python flask : No module named requests

I'm having trouble using requests module in my flask app. I have two files rest_server.py and independent.py at same directory level. The independent.py uses requests module and it executes correctly if I directly run it. But when I import independent.py in rest_server.py it shows following error `

import independent
  File "/home/satwik/Desktop/angelhack/independent.py", line 5, in <module>
    import requests
ImportError: No module named requests`

I've tried pip install requests and it shows requirement already satisfied. Also I've tried to import requests in rest_server.py and found it to execute correctly too. Here's my code

**independent.py **

`import json
import os
import sys
import requests
sys.path.append('/home/satwik/Desktop/angelhack/comprehensive_search')

** rest_server.py **

`#!flask/bin/python
import six
from flask import Flask, jsonify, abort, request, make_response, url_for
from flask.ext.httpauth import HTTPBasicAuth
import independent

app = Flask(__name__, static_url_path="")`

How should I fix this?

Upvotes: 2

Views: 11776

Answers (3)

Hossein Y
Hossein Y

Reputation: 9

hi i had same problem but i solved it :

after you activate venv env by this command . venv/bin/activate in this env u can type pip install requests

or

in ur project directory u can open pyvenv.cfg and turn

include-system-site-packages = false

to

include-system-site-packages = true

:)

Upvotes: 1

Aniket
Aniket

Reputation: 89

Whenever you do pip install <package>, it installs the package to a certain location. Add that location to the list of PATHs mentioned in your Environment Variables, and your problem will be solved.

Upvotes: 1

Daniele Barresi
Daniele Barresi

Reputation: 623

Why you get the "no module named ..." error

Your two files have one big difference: rest_server.py includes a shebang line, while independent.py doesn't.

When you say you directly execute the file independent.py, you type python independent.py (I'm assuming here, because you didn't specify that). That means you are executing with the system python interpreter, which will look for modules installed at system level. Systemwide you have the requests module installed, via pip install requests, so python finds it, imports the thing and happily runs your script.

When you execute the file rest_server.py, instead, you can do so calling the script's name: ./rest_server.py (assuming correct permissions settings). In this case, the first line #!flask/bin/python (the so called shebang line) instructs to use a different python interpreter, the one contained in the flask folder, which I assume contains a virtual environment.

You get the no module named requests because that module is not installed inside the flask virtual environment.

How you can fix the error

To fix the problem, just install the requests module inside the virtual environment.

You first activate the virtual environment and then install the module you need:

$ source flask/bin/activate
$ pip install requests

Then you can try execute ./rest_server.py again and the requests module should be properly imported.

For more on the shebang line: https://en.wikipedia.org/wiki/Shebang_(Unix)

For more on virtual environments: https://pypi.python.org/pypi/virtualenv

Upvotes: 6

Related Questions