Heroku error installing PIL dependency on Virtual Env - python

I'm having problems pushing my Django app to Heroku. There seems to be an error with installing PIL. I've traditionally had a problem with PIL b/c I'm on Windows, so using pip install or easy_install doesn't work because it can't find "vcvarsall.bat"
So as a quick solution, I went to this site and ran teh .exe version of PIL to install. I had problems getting PIL onto my virtual environment, so when creating the virtual environment, I use this
virtualenv --system-site-packages venv
Now I'm using
git push heroku master
and I'm getting this back
Downloading/unpacking PIL==1.1.7 (from -r requirements.txt)
Could not find any downloads that satisfy the requirement PIL==1.1.7 (line 2))
...
Heroku push rejected, failed to compile Python/Django app
! [remote rejected] m aster -> master (pre-receive hook declined)
error: failed to push some refs to 'git#heroku.com...'
How can I get past PIL?

I remember having this problem with pip finding PIL. Try using this line instead of PIL=1.1.7 in requirements.txt
http://effbot.org/media/downloads/Imaging-1.1.7.tar.gz
Also, you can always remove any module from requirements.txt and replace it with a copy of the module code in your Django root.
For example, with the PIL thing, you could have used that link to download the code, unzipped it, and pasted the PIL inside the resulting Imaging-1.1.7 folder into your Django project root. Would've worked just the same.
In general you want to avoid this if possible because it increases the size of your code deployment, but you can do it. It'll help you get around these issues so you can keep working for now at least.
One case where it might actually be preferable to include the code, rather than just using requirements.txt, is when you are working with a library or module that is poorly documented, and you want to have immediate and quick access to the source code from inside your editor. Probably wouldn't do it on a production server, but for dev that can be very convenient.

Related

Error in Installing spacy en_core_web_lg on Heroku app

Am deploying my ML model on Heroku using Django, I need en_core_web_lg for my application but couldn't install it
My requirements.txt is like:
..
..
djangorestframework==3.12.2
en-core-web-lg==2.3.1
en-core-web-sm==2.3.1
gunicorn==20.0.4
heroku==0.1.4
..
The Error is:
ERROR: Could not find a version that satisfies the requirement en-core-web-lg==2.3.1 (from -r /tmp/build_c3075f3c_/requirements.txt (line 14)) (from versions: none)
ERROR: No matching distribution found for en-core-web-lg==2.3.1 (from -r /tmp/build_c3075f3c_/requirements.txt (line 14))
! Push rejected, failed to compile Python app.
! Push failed
Your error is because there is no pip library named en-core-web-lg. Keep in mind that you cannot put en-core-web-lg in the requirements.txt since it is not a library that you can install using pip. They are models that spacy uses.
The accepted answer is fine. However, if you want a complete automatic deployment, you can also add something like this into your code:
try:
nlp = spacy.load("en_core_web_md")
except: # If not present, we download
spacy.cli.download("en_core_web_md")
nlp = spacy.load("en_core_web_md")
This will download the models automatically if they are not present. And, it will not require you to use the Heroku terminal. Only spacy on the requirements.txt file will be necessary.
Finally, keep in mind that spacy models are loaded in memory (RAM). The lg model (about 700MB) is too big to be fitted in a Free, Hobby or Standard 1X dynos (512MB of RAM). So it will fail to load and the dyno will be killed with R15 (https://devcenter.heroku.com/articles/error-codes#r15-memory-quota-vastly-exceeded).
More info about dynos: https://www.heroku.com/dynos
I don't know why it is not installing direct from requirements.txt file. There is another way it is not right way but the work will go on.
First you have to remove the package from requirements.txt file for which the error is coming.
Then push the app on Heroku, once your app have come to Heroku then write this code either from the terminal or from the Heroku dashboard.
If you are using terminal then:
heroku run bash
Then run:
pip install spacy
and then install which requiremts you want from spacy
python -m spacy download en_core_web_lg
if you are using Heroku dashboard then :
first go to your Heroku dashboard click on your app and then at top right click on More and select the Run Console
example image:
than this interface will come up
in this you have to click on bash or type bash and run
after this you can put same cammand and install.

Difference between installation of pip git+https and python setup.py

I am aware of this popular topic, however I am running into a different outcome when installing a python app using pip with git+https and python setup.py
I am building a docker image. I am trying to install in an image containing several other python apps, this custom webhook.
Using git+https
RUN /venv/bin/pip install git+https://github.com/alerta/alerta-contrib.git#subdirectory=webhooks/sentry
This seems to install the webhook the right way, as the relevant endpoint is l8r discoverable.
What is more, when I exec into the running container and doing a search for relevant files, I see the following
./venv/lib/python3.7/site-packages/sentry_sdk
./venv/lib/python3.7/site-packages/__pycache__/alerta_sentry.cpython-37.pyc
./venv/lib/python3.7/site-packages/sentry_sdk-0.15.1.dist-info
./venv/lib/python3.7/site-packages/alerta_sentry.py
./venv/lib/python3.7/site-packages/alerta_sentry-5.0.0-py3.7.egg-info
In my second approach I just copy this directory locally and in my Dockerfile I do
COPY sentry /app/sentry
RUN /venv/bin/python /app/sentry/setup.py install
This does not install the webhook appropriately and what is more, in the respective container I see a different file layout
./venv/lib/python3.7/site-packages/sentry_sdk
./venv/lib/python3.7/site-packages/sentry_sdk-0.15.1.dist-info
./venv/lib/python3.7/site-packages/alerta_sentry-5.0.0-py3.7.egg
./alerta_sentry.egg-info
./dist/alerta_sentry-5.0.0-py3.7.egg
(the sentry_sdk - related files must be irrelevant)
Why does the second approach fail to install the webhook appropriately?
Should these two option yield the same result?
What finally worked is the following
RUN /venv/bin/pip install /app/sentry/
I don't know the subtle differences between these two installation modes
I did notice however that /venv/bin/python /app/sentry/setup.py install did not produce an alerta_sentry.py but only the .egg file, i.e. ./venv/lib/python3.7/site-packages/alerta_sentry-5.0.0-py3.7.egg
On the other hand, /venv/bin/pip install /app/sentry/ unpacked (?) the .egg creating the ./venv/lib/python3.7/site-packages/alerta_sentry.py
I don't also know why the second installation option (i.e. the one creating the .egg file) was not working run time.

Use OpenCV on deployed Flask app (Heroku)

Hello I seem to be having trouble importing opencv on my deployed flask app on Heroku!
I've referred to similar posts such as this this
"ImportError: libSM.so.6: cannot open shared object file: No such file or directory " but can't seem to figure out the next steps on windows.
This is what I've done so far:
1. gone to Heroku -> App -> Settings -> Buildpacks -> added Python buildpack
2. Added a Aptfile.txt to my directory with the following packages on each line (read this somewhere not sure if it makes any sense)
libsm6 , libxrender1 ,libfontconfig1, libice6
Notes:
My openCV version-- opencv-python==3.4.3.18
I'm on windows so the sudo commands recommended in the other post answers dont work
Thanks in advance!
Use opencv-python-headless it's out of libSM6 dependency.
pip install opencv-python-headless
put this line in requirments.txt
opencv-python-headless==4.2.0.32

Deploying Python app to Heroku: "Could not import setuptools which is required to install from a source distribution."

Whenever I'm trying to push using the 'git push origin HEAD:master --force' command, I'm getting the following setuptools error (check out the screenshot): "Could not import setuptools which is required to install from a source distribution".
I thought it was related to the recent pip/setuptools update in official python buildpack.. so i made a fork of buildpack's repository and did a roll-back to the previous commits where old versions were used, but it didn't help. I tried to execute a 'purge-cache' command.. no luck. Then I tried to do a 'reset', change requirements.txt.. But it all just doesn't help to solve the problem. Would appreciate any help. Thanks!
Well, I finally found the reason. I had some weird pkg_resources.py module sitting in the root directory. Have no idea what is it for and why it was there (it's like a long-time project, so I didn't participate in adding that module). But the thing is that after I removed that crap - everything works like a charm

Heroku ImportError: No module named tweepy - but it's installed and runs fine locally

I've made a very simple Twitter bot in Python using the Tweepy library. Everything works beautifully locally and it tweets when I run the correct script exactly as it's supposed to. However, I'd like to automate this so that it tweets a couple of times a day, so have tried using the Heroku Scheduler for this.
I'm able to push to Heroku with no problems, but when I try to run the worker on Heroku I get this error message (which means of course the scheduler isn't working either since it's running the same process):
Running `worker` attached to terminal... up, run.8157
/app/.bash_profile: line 6: parts: command not found
Traceback (most recent call last):
File "/app/workspace/botlovesyou/lovescript.py", line 4, in <module>
import tweepy, time
ImportError: No module named tweepy
So, erm...what now? I've Googled and found this but am not sure how to actually fix it. This question looks deceptively similar but I don't have the other missing dependency that caused his issue.
I've tried running pip install tweepy again but of course it says 'Requirement already satisfied'. I've tried pip uninstalling it then pip installing it again - it reinstalls no problem, but I get the same error when it's on heroku. I've also tried installing the buildpack mentioned at the link above but it failed to build and wouldn't allow me to push so I removed it.
EDIT: I've now also tried git cloning and installing tweepy using setup.py. Again, installation ran with no errors but Heroku still says there's no module named Tweepy.
Tweepy is definitely installed in the site-packages directory. What am I missing?
Many thanks.
Problem resolved (this problem, anyway).
Install was aborting when it reached PIL in the requirements.txt file - it can't be installed via pip and was just skipping installing everything after it. I don't require PIL for this project so just removed it from requirements (I don't really know why it was in requirements in the first place, I think it was a nitrous.io preinstall). Very simple solution in this case.
Edited to add - on starting a new Python box on Nitrous, PIL was preinstalled correctly. So I think my initial install might have been corrupted.
Ugh. Edited again. Nitrous seems to have eaten PIL again, or maybe Heroku has. Same error. Again removed it from requirements.

Categories