Installing a Module on Heroku - django

I'm deploying something that has been running on my local and realized when I deployed that the module I was using wasn't installed on Heroku, thus I was getting an error like this:
...
from PIL import Image
ImportError: No module named PIL
I've tried:
heroku run pip install PIL
but I'm getting this:
ImportError: No module named setuptools.command
edit
So, I went to the heroku setup and mimicked the steps to install django and postgres. Essentially, I activated the environment and then ran
pip install PIL
This seemed to be doing the trick, I got a lot of readout and then it ended with a confirmation that PIL had been installed. But then again, it said it couldn't find PIL when I ran the new file.

Every command you run on Heroku is run in an isolated and ephemeral environment — any changes you make during heroku run are thrown away immediately when the processes completes.
To make PIL available to your application, you need to add it to requirements.txt instead.

Sometimes PIL package is added in your path but it's somewhere else than in site-packages. In this case you will be able to just import Image.
To make sure try something like this:
>>> import sys
>>> sys.path
[(...), '/usr/lib/python2.7/dist-packages/PIL', (...)]
>>> from PIL import Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PIL
>>> import Image
>>> Image
<module 'Image' from '/usr/lib/python2.7/dist-packages/PIL/Image.pyc'>
In my applications I use code like this:
try:
import Image, ImageDraw, ImageFont
except:
from PIL import Image, ImageDraw, ImageFont

Are you sure you have followed http://devcenter.heroku.com/articles/django#prerequisites and your virtualenv is loaded?
BTW, I recommend using Pillow instead of PIL.
Introduction
The fork author's goal is to foster packaging improvements via:
Publicized development and solicitation of community support.
Exploration of packaging problems within the fork, most noticably via
adding setuptools support but also via clean up & refactoring of
packaging code. Why a fork?
PIL is currently not setuptools compatible. Please see
http://mail.python.org/pipermail/image-sig/2010-August/006480.html for
a more detailed explanation. Also, PIL's current release/maintenance
schedule is not compatible with the various & frequent packaging
issues that have occured.

Even i was facing the same issue, I spent quite a lot of time.
I tried this
heroku run pip install PIL --app=your-app
error:
Running `pip install PIL` attached to terminal... up, run.1983
Downloading/unpacking PIL
Could not find any downloads that satisfy the requirement PIL
Some externally hosted files were ignored (use --allow-external PIL to allow).
Cleaning up...
No distributions at all found for PIL
Storing debug log for failure in /app/.pip/pip.log
But this helps like a charm :)
heroku run pip install Pillow --app=your-app

Related

Failed to load module pytesseract

I am trying capture text file from image and I am referring to this link https://www.simplifiedpython.net/how-to-extract-text-from-image-in-python/
So far I already done installing the pytesseract and Image library using this command:
pip install Image
pip install pytesseract
Already done downloading and install the PIL-1.1.7.win32-py2.7.exe
I tried now to run this on my script but still got error:
import pytesseract
try:
from PIL import Image
except ImportError:
import Image
Here is my error:
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: %1 is not a valid Win32 application.
Anyone know if there any prerequisites that I am missing now. TIA
I just reinstall the numpy and error got solve.

Installed python module in google cloud shell but python import is giving ImportError

I have tried installing scipy in Google Cloud Shell. The package is installed, but a python import is giving "ImportError: No module named scipy". Screenshot
I have problem only with scipy. Tensorflow and numpy are all working fine.
This problem should be similar to Installed packages disappeared in Google Cloud Shell.
The problem here is that scipy has not been installed. The process got killed at 99%. This is caused by how pip tries to install the package. I suspect that you're using a small VM instance which has memory limitations when pip tries to load the whole file to memory before installing it.
The solution is to install scipy with this command:
pip --no-cache-dir install scipy
Here you ask pip not to cache the file which should do the trick to install scipy on your Google Cloud VM. After the successful installation you should be able to import the scipy module as intended.

How to import modules into Jupyter notebook

So I am using anaconda and conda in a Windows OS.
And as you may know Jupyter gets installed automatically with Anaconda.
My python code runs normally when using the python command
python myfile.py
However when I try to run it through jupyter I receive the following error
ImportError Traceback (most recent call last)
<ipython-input-1-43605f892034> in <module>()
1 #!/usr/bin/env python
2 import os
----> 3 from gensim import corpora, models, similarities
4 from gensim.models import LsiModel
5 import logging
ImportError: No module named gensim
To troubleshoot I checked the following
Made sure to run jupyter notebook while enabling the activating the proper environment
Made sure the activated envrioment has the "gensim package" installed
So to solve this issue I need to install the missing library through jupyter itself
So I have to add and run the following command in a jupyter cell
pip install --upgrade gensim

How to import psycopg2 when installed - Windows

Can somebody please help me install this psycopg2 which I dont even know what it is. I have no python knowledge, I need this to run a code to connect to read data from txt to postgres.
Problem is, I have installed psycopg2 from exe and but when I run import psycopg2 in python shell I get an error:
import psycopg2
Traceback (most recent call last): File "", line 1, in
import psycopg2 ImportError: No module named psycopg2
i think Psycopg2 is not installed.
please install psycopg2 using pip
like:
pip install psycopg2
once done with this you can import
the pip install in windows doesn't seem to work well and ends up with the error you have mentioned. You can try the binaries provided in StickPeople website. These binaries install without problem and will allow you to access psycopg2. Please check the version of python you have installed with the version of the psycopg2 version. If they don't match up, it'll result in such errors. Even the architecture x86 or x64 matters.
C:\> easy_install psycopg2-2.6.1.win32-py2.7-pg9.4.4-release.exe, you can try this if the binary fails.

Running Django in Virtualenv on EC2 -- ImportError: No module named django.core.management

I developed a django application locally, in a git repo. I launched an EC2 instance for the project and I set up a virtualenv with (what I believe to be) the correct packages/dependencies. I then proceeded to clone my repo into the virtualenv. Right now, I'm having difficulty as I'm receiving the following errors:
I attempted to use python manage.py runserver example.com/8080 to test. I was sure to activate the virtualenv using source bin/activate, just like I did in my local virtualenv. When I call ... runserver I get the following error:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Here is what sudo pip freeze produces:
Warning: cannot find svn location for distribute==0.6.24dev-r0
Cheetah==2.4.4
Django==1.5.2
Fabric==1.8.0
GnuPGInterface==0.3.2
Landscape-Client==12.05
M2Crypto==0.21.1
PAM==0.4.2
PyYAML==3.10
South==0.8.2
Twisted-Core==11.1.0
Twisted-Names==11.1.0
Twisted-Web==11.1.0
apt-xapian-index==0.44
argparse==1.2.1
boto==2.2.2
chardet==2.0.1
cloud-init==0.6.3
command-not-found==0.2.44
configobj==4.7.2
## FIXME: could not find svn URL in dependency_links for this package:
distribute==0.6.24dev-r0
django-s3-folder-storage==0.1
django-storages==1.1.8
django-tastypie==0.10.0
ecdsa==0.9
euca2ools==2.0.0
gunicorn==18.0
httplib2==0.7.2
keyring==0.9.2
language-selector==0.1
launchpadlib==1.9.12
lazr.restfulclient==0.12.0
lazr.uri==1.0.3
medusa==0.5.4
meld3==0.6.5
oauth==1.0.1
paramiko==1.12.0
psycopg2==2.5.1
pyOpenSSL==0.12
pycrypto==2.4.1
pycurl==7.19.0
pyserial==2.5
python-apt==0.8.3ubuntu7.1
python-dateutil==2.1
python-debian==0.1.21ubuntu1
simplejson==2.3.2
six==1.4.1
supervisor==3.0a8
ufw==0.31.1-1
unattended-upgrades==0.1
virtualenv==1.10.1
wadllib==1.3.0
wsgiref==0.1.2
zope.interface==3.6.1
...and this is my ./manage.py file:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Any thoughts on how I can fix this error? I tried to change #!/usr/bin/env python to #!/var/www/paletto-env/bin python, trying to direct it to my actual python path, but it did not lend to any apparent change, so I changed it back.
Thanks for the help.
Looks like you're environment can't find Django, even though it's clearly in your pip freeze.
Try opening a Python (not Django) shell from your virtual environment and entering:
import django
django.VERSION
If you run into the same error, there's probably an issue with your Django install. Your virtual environment probably can't find it. You could try modifying the path settings, or just reinstall Django.
If you can successfully import Django, or if you tried reinstalling and it doesn't work, you may have a permission problem. Ensure that the user responsible for running the server has access to wherever your python libraries are stored from the virtualenv.
I had pretty much the exact same problem as you and this is how I solved it (Disclaimer: I'm not sure if this is the absolute correct way but it worked for me and everything seems correct).
Short Answer:
Try pip install (package) instead of sudo pip install (package)
Long Answer:
I gave the Django complete install doc (See here) a quick read through and came across a bit that basically said you dont need super user privileges when using pip in the virtualenv. I just assumed that either would be fine but I now realize that's probably not the case.
I suspect that because I entered sudo pip install django it installed it somewhere above my local bin in my virtualenv for the project. I suspect this because when I enter python manage.py runserver I get an error; however, when I enter sudo python manage.py runserver everything functions properly.
Also, typing sudo pip freeze reveals my larger library whereas pip freeze reveals my local library for my virtualenv.
I didn't want to type sudo (do stuff) before everything and I wanted a nice, clean and proper virtualenv so I just reinstalled django but to my local virtual env with pip install django and now django is in the proper virtualenv library, pip freeze returns the proper contents, and python manage.py runserver functions properly!
django-admin.py startproject by default creates the shebang in manage.py with #!/usr/bin/env python.
If this is not the path to your python executable, or if you use python3, just edit the manage.py to reflect this.
Reinstall all python packages angain after you have activated your virtualenv.
Maybe the Django has lost some plugin with pip, so we can install Django with Tarball:
Go https://www.djangoproject.com/download/1.6.5/tarball/
Download Django-*.tar.gz
install it.
$ tar zxvf Django-1.6.5.tar.gz
$ cd Django-1.6.5/
$ python setup.py install
more ... (https://stackoverflow.com/a/24323774/686105