Python cannot find flask package which is installed - python

When I run my flask project on my server, I get the following error:
Traceback (most recent call last):
File "File.py", line 1, in <module>
from flask import Flask, request
ImportError: No module named 'flask'
But I have flask installed!
$sudo pip install flask
Requirement already satisfied: flask in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: itsdangerous>=0.21 in /usr/local/lib/python2.7/dist-packages (from flask)
Requirement already satisfied: click>=2.0 in /usr/local/lib/python2.7/dist-packages (from flask)
Requirement already satisfied: Werkzeug>=0.7 in /usr/local/lib/python2.7/dist-packages (from flask)
Requirement already satisfied: Jinja2>=2.4 in /usr/local/lib/python2.7/dist-packages (from flask)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python2.7/dist-packages (from Jinja2>=2.4->flask)
This is driving me crazy. I've tried what other SO answers have suggested, including using a venv, but they all produce this same error. Anyone have a suggestion?
$ which python
/usr/bin/python
$ which python3
/usr/bin/python3
I'm running python 3.5.2

I'm running python 3.5.2
As the text says, Flask is not installed there.
Requirement already satisfied: flask in /usr/local/lib/python2.7/dist-packages
This folder is also the system Python, not the virtualenv, if you are using one. You can set virtualenv to use the system installed packages, but again, that path is not Python3.
The root issue is that using sudo executed pip under a different user account
Either use Python2, or install using pip3 or python3 -m pip and you shouldn't need sudo

Related

ModuleNotFoundError: No module named 'flask_cors' on python anywhere

To state from the get go this is no criticism of pythonanywhere, but I want to run a script that used to work using the line:
from flask_cors import CORS
But I get the following error mesage:
ModuleNotFoundError: No module named 'flask_cors'
Then tried to install in my version of python:
pip3.9 install Flask-Cors
The result was:
Defaulting to user installation because normal site-packages is not
writeable Looking in links: /usr/share/pip-wheels Requirement already
satisfied: Flask-Cors in ./.local/lib/python3.9/site-packages (3.0.10)
Requirement already satisfied: Six in
/usr/local/lib/python3.9/site-packages (from Flask-Cors) (1.16.0)
Requirement already satisfied: Flask>=0.9 in
/usr/local/lib/python3.9/site-packages (from Flask-Cors) (2.0.0)
Requirement already satisfied: Werkzeug>=2.0 in
/usr/local/lib/python3.9/site-packages (from Flask>=0.9->Flask-Cors)
(2.0.1) Requirement already satisfied: itsdangerous>=2.0 in
/usr/local/lib/python3.9/site-packages (from Flask>=0.9->Flask-Cors)
(2.0.1 ) Requirement already satisfied: click>=7.1.2 in
/usr/local/lib/python3.9/site-packages (from Flask>=0.9->Flask-Cors)
(7.1.2) Requirement already satisfied: Jinja2>=3.0 in
/usr/local/lib/python3.9/site-packages (from Flask>=0.9->Flask-Cors)
(3.0.1) Requirement already satisfied: MarkupSafe>=2.0 in
/usr/local/lib/python3.9/site-packages (from
Jinja2>=3.0->Flask>=0.9->Flask-C ors) (2.0.1) 15:33 ~$ pip show
Flask-Cors
Name: Flask-Cors Version: 3.0.10 Summary: A Flask extension adding a decorator for CORS support Home-page:
https://github.com/corydolphin/flask-cors Author: Cory Dolphin
Author-email: corydolphin#gmail.com License: MIT Location:
/home/elksie5000/.local/lib/python3.9/site-packages Requires: Six,
Flask
What gives and how do I fix it?
I think you should try the following:
Go to your Bash Console in "Consoles" menu.
Open the bash control
Select your virtualenv:
For example my virtualenv name is flaskapp, I would wirte "workon
flaskapp" in the bash console.
Now type in your desired command
pip3.9 install flask-cors in your case (try in lower case)
Make sure you are working in the same virtualenv and the same python version
(I think you want to make an API using cors maybe)

pytorch,opencv installation problem in python2 having cv library installed in python3

haiii i am running python code in ubuntu 18.04
i needed opencv libarary for the code so i installed opencv using python3
pip3 install opencv-python
but the code requires and compatible with python 2
so i tried to install the library using just pip instead of pip3
when i tried to install opencv using pip it is saying
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: opencv-python in /home/sri/.local/lib/python3.6/site-packages (4.4.0.46)
Requirement already satisfied: numpy>=1.13.3 in /home/sri/.local/lib/python3.6/site-packages (from opencv-python) (1.19.4)
but running my file python train.py shows no module named cv2
some body please help me to solve this problem
same going with my pytorch library also
Traceback (most recent call last):
File "train.py", line 6, in <module>
from model import CANNet
File "/home/sri/Downloads/Context-Aware-Crowd-Counting-master/model.py", line 1, in <module>
import torch.nn as nn
ImportError: No module named torch.nn
so i tried installing pytorch
pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
Defaulting to user installation because normal site-packages is not writeable
Looking in links: https://download.pytorch.org/whl/torch_stable.html
Requirement already satisfied: torch==1.7.0+cpu in /home/sri/.local/lib/python3.6/site-packages (1.7.0+cpu)
Requirement already satisfied: torchvision==0.8.1+cpu in /home/sri/.local/lib/python3.6/site-packages (0.8.1+cpu)
Requirement already satisfied: torchaudio==0.7.0 in /home/sri/.local/lib/python3.6/site-packages (0.7.0)
Requirement already satisfied: numpy in /home/sri/.local/lib/python3.6/site-packages (from torch==1.7.0+cpu) (1.19.4)
Requirement already satisfied: future in /home/sri/.local/lib/python3.6/site-packages (from torch==1.7.0+cpu) (0.18.2)
Requirement already satisfied: dataclasses in /home/sri/.local/lib/python3.6/site-packages (from torch==1.7.0+cpu) (0.8)
Requirement already satisfied: typing-extensions in /home/sri/.local/lib/python3.6/site-packages (from torch==1.7.0+cpu) (3.7.4.3)
Requirement already satisfied: pillow>=4.1.1 in /home/sri/.local/lib/python3.6/site-packages (from torchvision==0.8.1+cpu) (8.0.1)
You are troubleshooting with your versions. You say your code is not workable for python3 so you tried to run it on python2. It seems that your pip is linking against your python 3.6 Version instead of python 2.X. The best way to handle these problems are virtual environments:
Python 3.X: https://docs.python.org/3/library/venv.html
Python 2.X: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
Inside an activated Environment the command python and pip are linked to the version of the environment.

Locate where conda placed a python package I installed

A python newbie question:
On an Ubuntu system, I installed a python package using sudo pip install. (The package is called hlmm)
I'm using the miniconda3 environment and the only environment variable specified in my .bashrc is:
export PATH="/home/<my_username>/miniconda3/bin:$PATH"
Now I want to run a test script from the package I installed, which is located under a tests folder relative to where that package was installed.
I used pip list | xargs -exec pip show to look for the location of that package but it doesn't show up.
I also tried in python to import the package, using:
import hlmm
And I'm getting:
>>> import hlmm
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'hlmm'
If I redo sudo pip install hlmm I get:
Requirement already satisfied: hlmm in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: scipy in /usr/local/lib/python2.7/dist-packages (from hlmm)
Requirement already satisfied: numpy in /usr/local/lib/python2.7/dist-packages (from hlmm)
Requirement already satisfied: pysnptools in /usr/local/lib/python2.7/dist-packages (from hlmm)
Requirement already satisfied: pandas>=0.19.0 in /usr/local/lib/python2.7/dist-packages (from pysnptools->hlmm)
Requirement already satisfied: pytz>=2011k in /usr/local/lib/python2.7/dist-packages (from pandas>=0.19.0->pysnptools->hlmm)
Requirement already satisfied: python-dateutil>=2.5.0 in /usr/local/lib/python2.7/dist-packages (from pandas>=0.19.0->pysnptools->hlmm)
Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/dist-packages (from python-dateutil>=2.5.0->pandas>=0.19.0->pysnptools->hlmm)
And, I do see the hlmm folder under /usr/local/lib/python2.7/dist-packages, but cannot find that tests folder:
ls -1 /usr/local/lib/python2.7/dist-packages/hlmm
__init__.py
__init__.pyc
hetlm.py
hetlm.pyc
hetlmm.py
hetlmm.pyc
How do I find its location? And, is the package really successfully installed despite the import hlmm error?
This was getting too long to be a comment, so I'm posting it as an answer.
Yeah, it sounds like pip installed it from your python 2.7 version. You should read this post: How to install PyPi packages using anacaonda conda command and this article: https://www.anaconda.com/using-pip-in-a-conda-environment/ to decide how you want to proceed. I generally use conda when possible, and pip if conda doesn't have a build. You might want to run pip uninstall hlmm then activate your conda env and use pip3 install hlmm. But, I think you should decide the best way to manage your environments after doing research.

from google.cloud import language ModuleNotFoundError: No module named 'google.cloud'

I had a code that I was able to run last year using Google Cloud Natural Language. I am not able to this year due to a library problem. I used the following methods to install it. However, not successful. How should install it?
[jalal#goku sentiment]$ pip install --user --upgrade google-cloud
Collecting google-cloud
Downloading https://files.pythonhosted.org/packages/ba/b1/7c54d1950e7808df06642274e677dbcedba57f75307adf2e5ad8d39e5e0e/google_cloud-0.34.0-py2.py3-none-any.whl
Installing collected packages: google-cloud
Successfully installed google-cloud-0.34.0
[jalal#goku sentiment]$ python sentiment.py
Traceback (most recent call last):
File "sentiment.py", line 20, in <module>
from google.cloud import language
ModuleNotFoundError: No module named 'google.cloud'
[jalal#goku sentiment]$ python -m pip install google-cloud
Requirement already satisfied: google-cloud in /home/grad3/jalal/.local/lib/python3.6/site-packages (0.34.0)
The error is:
$ python sentiment.py
Traceback (most recent call last):
File "sentiment.py", line 20, in <module>
from google.cloud import language
ModuleNotFoundError: No module named 'google.cloud'
A little about my system setup:
$ which python
/scratch/sjn-p3/anaconda/anaconda3/bin/python
$ python
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 18:10:19)
[GCC 7.2.0] on linux
$ lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.6.1810 (Core)
Release: 7.6.1810
Codename: Core
Also, I find it very weird when I am installing the pip package with Anacoda pip why does it install it into native Python and not Anaconda Python?
$ /scratch/sjn-p3/anaconda/anaconda3/bin/pip install --user --upgrade google-cloud
Requirement already up-to-date: google-cloud in /home/grad3/jalal/.local/lib/python3.6/site-packages (0.34.0)
The issue is you are using the --user flag and pip but wish to run the Anaconda distribution of Python.
If you do python -c 'import site; print(site.USER_BASE)' from the CLI it will return /home/grad3/jalal/.local hence why packages are installed there when you pass the --user flag to pip. Since you want to use the Anaconda distribution you should stay consistent and use their version of a package manager, which would be conda in this case, which you can find about more --> here.
However, I highly recommend using virtual environments so that this issue can be avoided in the future. You can find out more about Anaconda virtual environments over --> here.
If you wish to keep using pip along with Anaconda you can modify your target directory in the ~/.pip/pip.conf file to point at the location you want. Probably something along these lines will work:
[global]
target=/scratch/sjn-p3/anaconda/anaconda3/lib/python{version}/site-packages
Solved the problem by the following command. You have to install the specific product from the Google Cloud (in this case Natural Language):
$ pip install --user --upgrade google-cloud-language
Collecting google-cloud-language
Using cached https://files.pythonhosted.org/packages/b1/3a/6f5808421aea97363cb19a095f73ed6eed12bbaee1e67f2145af7125585d/google_cloud_language-1.1.1-py2.py3-none-any.whl
Collecting google-api-core[grpc]<2.0.0dev,>=1.6.0 (from google-cloud-language)
Using cached https://files.pythonhosted.org/packages/7d/73/e4877e921fe59307ec6b1b0b0c2ad9fde2d1c6bab8dd06ec913891a20dc6/google_api_core-1.8.2-py2.py3-none-any.whl
Requirement already satisfied, skipping upgrade: protobuf>=3.4.0 in /scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language) (3.6.1)
Requirement already satisfied, skipping upgrade: pytz in /scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language) (2017.3)
Requirement already satisfied, skipping upgrade: six>=1.10.0 in /scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language) (1.11.0)
Requirement already satisfied, skipping upgrade: requests<3.0.0dev,>=2.18.0 in /home/grad3/jalal/.local/lib/python3.6/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language) (2.21.0)
Collecting google-auth<2.0dev,>=0.4.0 (from google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language)
Using cached https://files.pythonhosted.org/packages/c5/9b/ed0516cc1f7609fb0217e3057ff4f0f9f3e3ce79a369c6af4a6c5ca25664/google_auth-1.6.3-py2.py3-none-any.whl
Collecting googleapis-common-protos!=1.5.4,<2.0dev,>=1.5.3 (from google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language)
Requirement already satisfied, skipping upgrade: setuptools>=34.0.0 in /scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language) (38.4.0)
Requirement already satisfied, skipping upgrade: grpcio>=1.8.2; extra == "grpc" in /scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language) (1.15.0)
Requirement already satisfied, skipping upgrade: idna<2.9,>=2.5 in /scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language) (2.6)
Requirement already satisfied, skipping upgrade: urllib3<1.25,>=1.21.1 in /scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language) (1.22)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language) (2018.10.15)
Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in /scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language) (3.0.4)
Collecting pyasn1-modules>=0.2.1 (from google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language)
Using cached https://files.pythonhosted.org/packages/da/98/8ddd9fa4d84065926832bcf2255a2b69f1d03330aa4d1c49cc7317ac888e/pyasn1_modules-0.2.4-py2.py3-none-any.whl
Collecting cachetools>=2.0.0 (from google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language)
Using cached https://files.pythonhosted.org/packages/39/2b/d87fc2369242bd743883232c463f28205902b8579cb68dcf5b11eee1652f/cachetools-3.1.0-py2.py3-none-any.whl
Collecting rsa>=3.1.4 (from google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language)
Using cached https://files.pythonhosted.org/packages/02/e5/38518af393f7c214357079ce67a317307936896e961e35450b70fad2a9cf/rsa-4.0-py2.py3-none-any.whl
Collecting pyasn1<0.5.0,>=0.4.1 (from pyasn1-modules>=0.2.1->google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.6.0->google-cloud-language)
Using cached https://files.pythonhosted.org/packages/7b/7c/c9386b82a25115cccf1903441bba3cbadcfae7b678a20167347fa8ded34c/pyasn1-0.4.5-py2.py3-none-any.whl
Installing collected packages: pyasn1, pyasn1-modules, cachetools, rsa, google-auth, googleapis-common-protos, google-api-core, google-cloud-language
The scripts pyrsa-decrypt, pyrsa-encrypt, pyrsa-keygen, pyrsa-priv2pub, pyrsa-sign and pyrsa-verify are installed in '/home/grad3/jalal/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed cachetools-3.1.0 google-api-core-1.8.2 google-auth-1.6.3 google-cloud-language-1.1.1 googleapis-common-protos-1.5.9 pyasn1-0.4.5 pyasn1-modules-0.2.4 rsa-4.0
$ vi ~/.bashrc
Added this line to the end of my ~/.bashrc (or whatever it is told to you):
export PATH=/home/grad3/jalal/.local/bin:$PATH
$ source ~/.bashrc

Python: "No module named 'requests' " after pip3 install requests in pipenv

I'm trying to install the library requests in pipenv, with Python 3. I've tried:
pip install requests, before forgetting that pip installs only for Python 2, leading to,
pip3 install requests, which returns assuringly
Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (2.19.1)
Requirement already satisfied: idna<2.8,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (2.7)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (1.23)
Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (2018.8.24)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (3.0.4)
When I run my python files with import requests, everything works, but when I run pipenv run python mypythonscript.py, I get the error ModuleNotFoundError: No module named 'requests'. Please advise what I'm missing.
The problem here is that pip3 install requests runs outside of pipenv's virtualenv, it is installed outside of virtualenv, and thus requests library is not found from code running inside the virtualenv.
The correct way to install the library into the virtualenv is pipenv install requests, which will also add requests to your Pipfile. If you want to install it temporarily without saving into the Pipfile, you can run pipenv run pip install requests.

Categories

Resources