installing flask in Python on Heroku - python

I'm trying to follow this guide.
I made virtualenv and installed flask in it:
Requirement already satisfied (use --upgrade to upgrade): Flask in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied (use --upgrade to upgrade): Werkzeug>=0.7 in /usr/local/lib/python2.7/dist-packages (from Flask)
Requirement already satisfied (use --upgrade to upgrade): Jinja2>=2.4 in /usr/local/lib/python2.7/dist-packages (from Flask)
After pip freeze > requirements.txt the file contains only:
argparse==1.2.1
distribute==0.6.28
wsgiref==0.1.2
And there is no Flask package. I'm trying to run simple example and got:
ImportError: No module named flask
How to install the package properly?

You need to run pip freeze > reuirements.txt inside your virtualenv. Thus, make sure that your virtualenv was activated correctly. As the Heroku documentation is mostly written from Linux point of view, this may be a little tricky especially on Windows (see a related question):
Windows command-prompt activation is done using venv\Scripts\activate.bat
If you are using Windows PowerShell, you need to run venv\Scripts\activate.ps1 instead.
Note that you need to activate virtualenv for sudo as well. Example:
sudo bash
source venv/bin/activate
pip install Flask
However one of the points in virtualenv is that you don't need sudo. Thus, you could simply omit using sudo and simply just run:
source venv/bin/activate
pip install Flask

Related

pipenv install django==2.1 not working on google collab

I have already installed pipenv using,
pip install pipenv on the directory which was installed successfully. But
pipenv install Django==2.1 does not seem to work. In fact, no command is getting executed using pipenv.
pip install pipenv
Collecting pipenv
Downloading https://files.pythonhosted.org/packages/13/b4/3ffa55f77161cff9a5220f162670f7c5eb00df52e00939e203f601b0f579/pipenv-2018.11.26-py3-none-any.whl (5.2MB)
100% |████████████████████████████████| 5.2MB 5.5MB/s
Requirement already satisfied: setuptools>=36.2.1 in /usr/local/lib/python3.6/dist-packages (from pipenv) (40.9.0)
Collecting virtualenv-clone>=0.2.5 (from pipenv)
Downloading https://files.pythonhosted.org/packages/ba/f8/50c2b7dbc99e05fce5e5b9d9a31f37c988c99acd4e8dedd720b7b8d4011d/virtualenv_clone-0.5.3-py2.py3-none-any.whl
Requirement already satisfied: certifi in /usr/local/lib/python3.6/dist-packages (from pipenv) (2019.3.9)
Requirement already satisfied: pip>=9.0.1 in /usr/local/lib/python3.6/dist-packages (from pipenv) (19.0.3)
Collecting virtualenv (from pipenv)
Downloading https://files.pythonhosted.org/packages/33/5d/314c760d4204f64e4a968275182b7751bd5c3249094757b39ba987dcfb5a/virtualenv-16.4.3-py2.py3-none-any.whl (2.0MB)
100% |████████████████████████████████| 2.0MB 17.1MB/s
Installing collected packages: virtualenv-clone, virtualenv, pipenv
Successfully installed pipenv-2018.11.26 virtualenv-16.4.3 virtualenv-clone-0.5.3
pipenv install Django==2.1
File "", line 1
pipenv install Django==2.1
^
SyntaxError: invalid syntax
*
Anything on Google Colab suggestions would be very helpful!
*
pip is a shell command. I assume Google Colab has made pip available as a special command(*) (without the % syntax), but that doesn't mean any other shell command works as such. That is why you get a SyntaxError: it is not recognised as normal Python syntax.
You can work around it by using !pipenv install 'Django==2.1' (the single quotes may not be necessary, but the exclamation work is).
I doubt, however, that you really need a virtual environment in the first place. This is not on your regular machine, where you may want to keep dependencies apart. Instead, just start a new notebook for a new project, and install Django as normal: pip install Django. I wouldn't bother with pipenv and virtual environments inside notebooks. There doesn't seem to be any use for it.
(*) Try, for example, pip?, and note the help lists its usage with the %pip syntax. I assume that, if it's not ambiguous, Colab will use %pip when called as pip. The same works for, for example, ls instead of %ls.

ImportError: No module named 'pandas' (inside virtualenv)

I created a virtual environment named quora for python.
I installed wheel and then pandas as instructed.
I cant get pandas to work for some reason.
Can someone help me.
I have tried all the other solutions available to similar questions on this website. Still no use.
(quora) (jessie)griffith#localhost:~/environments$ sudo pip install wheel
Requirement already satisfied: wheel in /usr/lib/python2.7/dist-packages
(quora) (jessie)griffith#localhost:~/environments$ sudo pip install pandas
Requirement already satisfied: pandas in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: pytz>=2011k in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: numpy>=1.7.0 in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: python-dateutil in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/dist-packages (from python-dateutil->pandas)
(quora) (jessie)griffith#localhost:~/environments$ python getdata.py
Traceback (most recent call last):
File "getdata.py", line 2, in <module>
import pandas as pd
ImportError: No module named 'pandas'
I had this problem in a virtualenv with pip3 and pandas, tried all these previous answers, none of which actually work. but you can use easy_install pandas. et voilà.
Don't use sudo in a virtualenv — sudo pip install installs packages into global site-packages, not in virtualenv.
Either install pandas in the virtual environment (pip install after activating venv) or enable access to the global packages (recreate venv with option --system-site-packages or use command toggleglobalsitepackages from virtualenvwrapper).
I had the same problem. I fixed it by deleting my virtualenv directory and creating a new environment.
Check "which python" you are running using that command. You may need to export PATH to the python env instead of your default python which might be /usr/lib/bin. It might be installed in your quora env but the python that is being picked up is different and that doesn't have pandas
go to pyvenv.cfg and change
include-system-site-packages = false
to
include-system-site-packages = true
I tried all the answers before in my archlinux machine but none worked. But this post from Nikolai Janakiev helped me find a good solution.
Solution
Don't forget to first activate the virtual env, mine is named .venv:
$ source .venv/bin/activate
(.venv) $ python3 -m pip install ipykernel
Pick any arbitrary name and replace with NEW_KERNEL, this shows up in your jupyter notebook with the same name:
(.venv) $ python3 -m ipykernel install --name=NEW_KERNEL
And you're done!
Creating Virtual Environments
If you're not already familiar with setting up a virtual environment, here is the official guide.
Let's suppose you want to create a virtual environment under the name .venv. I use the . prefix to hide it by default.
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python3 -m pip install pandas
And voila! You have access to the pandas package in .venv environment.
Using Virtual Environments in Jupyter Notebooks
If you're wondering how to activate the virtual environment in a jupyter-notebook, just follow the Solution section above, and then open up the notebook, and click on Kernel then Change Kernel to NEW_KERNEL or the name you picked in the Solution section.

Python packages hash not matching whilst installing using pip

I am using pip to install all my python packages but get error as shown in the trace below. What is the problem and how can I solve it?
usr#comp:~$ pip install flask
Collecting flask
Using cached Flask-0.11.1-py2.py3-none-any.whl
Collecting itsdangerous>=0.21 (from flask)
Using cached itsdangerous-0.24.tar.gz
Collecting click>=2.0 (from flask)
Using cached click-6.6.tar.gz
Collecting Werkzeug>=0.7 (from flask)
Using cached Werkzeug-0.11.11-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): Jinja2>=2.4 in /usr/lib/python2.7/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): MarkupSafe in /usr/lib/python2.7/dist-packages (from Jinja2>=2.4->flask)
THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
Werkzeug>=0.7 from https://pypi.python.org/packages/a9/5e/41f791a3f380ec50f2c4c3ef1399d9ffce6b4fe9a7f305222f014cf4fe83/Werkzeug-0.11.11-py2.py3-none-any.whl#md5=c63a21eedce9504d223ed89358c4bdc9 (from flask):
Expected md5 c63a21eedce9504d223ed89358c4bdc9
Got 13a168aafcc43354b6c79ef44bb0dc71
There is a similar problem (Why does pip fail with bad md5 hash for package?) from 2013 the solution that I tried that worked for me is this:
sudo pip install --no-cache-dir flask
given by attolee
The problem here is the Python package was updated with new hash value while pip was trying to install the Python package using the old hash value cached in pip cache directory. This cache needs to be purge before the pip install attempt. So the full solution is:
python -m pip cache purge
python -m pip install <package>
--no-cache-dir did not work for me in raspberry pi 4 at first.
Found that the problem was due to unexpected network change/failure during pip installation
I had to download the broken .whl file manually with wget
and install it like below:
sudo pip install scipy-1.3.0-cp37-cp37m-linux_armv7l.whl
followed by
sudo pip install --no-cache-dir keras
Then it worked.
Looks like a cache problem, the cached package is different from REQUIREMENTS.
Perhaps caused by last updates interruption.
I did this which fixed my problem:
rm ~/.cache/pip -rf
You need to upgrade your pip into the newer version:
Using this command:
python -m pip install -upgrade pip
for Mac/Linux operating system and use
python -m pip install --upgrade tensorflow
for Windows to update your pip. Then run your command
pip install flask
In case you got this error while using pipenv try
$ pipenv --clear
$ pipenv lock
$ pipenv install
first, try to upgrade your pip then install the library
python -m pip install -upgrade pip
if it didn't work just try to install it without the cash
pip install --no-cache-dir the_library_name
I got the error during installing panads
You need to remove the cache and reinstall .
pip install --no-cache-dir flask
I had a similar issue for a different module. It was caused by network failure. My fix was nothing complex but another attempt at installing it and it worked.
maybe pipiserver(where you pip install from) upload a pkg for example flask-1.0.0.tar.gz, and rm is upload a new flask-1.0.0.tag.gz,if new pkg code has changed ,the hash must be different,there is two ways:
installl an older pkg version =, pip install flask==0.0.9
wait new pkg release flask==1.0.1 or cache expiration.
I have tried to clear pip cache with "-m pip cache purge" and using the "--no-cache-dir" argument but it was not helping.
In my case it was VPN being active during the attempts to install the package. As soon as I have turned it off everything worked as expected.

python-Can't use modules installed by Homebrew?

I want to use the essentia module in my python virtualenv and the version of python is 2.7.6.
After I executed
brew tap MTG/essentia
I can't find this module in my Pycharm.
I've also tried to install other package such as matplotlib using
sudo pip install matplotlib
under my virtualenv path.
The directory '/Users/username/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/username/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied (use --upgrade to upgrade): matplotlib in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.5 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): tornado in /Library/Python/2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): pyparsing>=1.5.6 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Collecting nose (from matplotlib)
/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Downloading nose-1.3.7-py2-none-any.whl (154kB)
100% |████████████████████████████████| 155kB 46kB/s
Requirement already satisfied (use --upgrade to upgrade): backports.ssl-match-hostname in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied (use --upgrade to upgrade): singledispatch in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied (use --upgrade to upgrade): certifi in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied (use --upgrade to upgrade): backports-abc>=0.4 in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied (use --upgrade to upgrade): six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from singledispatch->tornado->matplotlib)
Installing collected packages: nose
Successfully installed nose-1.3.7
You are using pip version 7.1.2, however version 8.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
I checked in Pycharm, however, in the interpreter there was no matplotlib module.
How can I install packages in my virtualenv?
If the pip package is not available, you can find the brew installation in your local python directory.
Quick Fix:
Install essentia to your local environment.
Append the local essentia package to your virtual environment
sys.path.append('/usr/local/lib/python2.7/site-packages')
import essentia
Long Fix:
If you want to fully contain the packages in your virtual environment, try copying the local site-packages/essentia folder to your virtual environment
Copy over any other dependencies such as site-packages/numpy. Check brew's Cellar (/usr/local/Cellar) for other dependencies
You should not install python modules with brew, instead you should use pip. The following method has worked for me in OSX.
Desktop $ virtualenv myproject # Create the virtual environment
Desktop $ cd myproject # Change into that directory
Desktop $ source bin/activate # Enter the virtual environment
(myproject) $ sudo pip install matplotlib # Install matplotlib
(myproject) $ deactivate # Exit from the virtual environment
Look here for a further explanation of virtualenv.

Python packages not installing in virtualenv using pip

I'm having trouble installing twisted
pip --version
pip 1.1 from
/home/chris/GL/GLBackend/glenv/lib/python2.7/site-packages/pip-1.1-py2.7.egg
(python 2.7)
Create a virtual environment
chris#chris-mint ~/GL/GLBackend $ sudo virtualenv -p python2.7 glenv
Running virtualenv with interpreter /usr/bin/python2.7 New python
executable in glenv/bin/python2.7 Also creating executable in
glenv/bin/python Installing
distribute.............................................................................................................................................................................................done.
Installing pip...............done.
Just in case, I'll enable all permissions
chris#chris-mint ~/GL/GLBackend $ sudo chmod -R 777 glenv
chris#chris-mint ~/GL/GLBackend $ source glenv/bin/activate
(glenv)chris#chris-mint ~/GL/GLBackend $ pip freeze
argparse==1.2.1 distribute==0.6.24 wsgiref==0.1.2
twisted is not listed here as installed
(glenv)chris#chris-mint ~/GL/GLBackend $ sudo pip install twisted
Requirement already satisfied (use --upgrade to upgrade): twisted in
/usr/local/lib/python2.7/dist-packages Requirement already satisfied
(use --upgrade to upgrade): zope.interface>=3.6.0 in
/usr/local/lib/python2.7/dist-packages (from twisted) Requirement
already satisfied (use --upgrade to upgrade): distribute in
/usr/local/lib/python2.7/dist-packages (from
zope.interface>=3.6.0->twisted) Cleaning up... (glenv)chris#chris-mint
~/GL/GLBackend $ pip uninstall twisted Cannot uninstall requirement
twisted, not installed Storing complete log in
/home/chris/.pip/pip.log
But when I install it it says that its already installed.
Force the install:
sudo pip install -I twisted
Downloading/unpacking twisted Downloading Twisted-12.3.0.tar.bz2
(2.6Mb): 2.6Mb downloaded Running setup.py egg_info for package
twisted
. . .
Successfully installed twisted zope.interface distribute Cleaning
up...
And yet it still isn't installed
(glenv)chris#chris-mint ~/GL/GLBackend $ pip freeze
argparse==1.2.1 distribute==0.6.24 wsgiref==0.1.2
**When I try running Python scripts which use twisted, I get an error saying that twisted is not installed. That is:
ImportError: No module named twisted.python**
The problem here is that you're using sudo when you shouldn't be. And that's causing pip to try to install into /usr/local/lib instead of ~/glenv/lib. (And, because you used sudo, it's successfully doing so, but that doesn't help you, because you're not allowing system site-packages in your venv.)
There are multiple reasons sudo pip could lead to this behavior, but the most likely is this: On most systems (including the various Mac and RHEL/CentOS boxes I have immediate access to), the sudoers file will reset your environment, then add back in a handful of whitelisted environment variables. This means that when you sudo pip, it will not see the environment variables that virtualenv sets up, so it will fall back to doing the default thing and install into your system Python, instead of your venv.
But really, it doesn't matter why this is happening. The answer is the same: just do pip install instead of sudo pip install.
Note that you also want to remove the sudo on the virtualenv call, as this will probably cause the venv to be set up incorrectly (which is why you need the sudo chmod, which wouldn't be necessary otherwise). The whole point of installing things under your user home directory is that you can do it with your normal user permissions.
As a side note, you also may want to upgrade to a newer virtualenv/pip, as 1.8 and 1.2 have some bug fixes and improvements. But I verified that I get exactly the same problem as you even with the latest (1.8.4 and 1.2.1) versions, so I don't think that's relevant here.
The sudo pip is causing the problem here. It will install the package in your system instead of the virtual environment you created. So when it says the requirement is already satisfied. Try to look add the directory it is pointing to. Which is in your case, while you were trying to install, was /usr/local/lib/python2.7/dist-packages
If it's inside something like /usr/local/lib/..., which does not points to your virtualenv folder then it is installed in your system. Otherwise, in correct scenario it will look something like this /usr/local/lib/..../<name of your virtualenv>/lib.
You can always use commands like which python & which pip to see if they both are using the placeholder of our virtual environment.

Categories