Django project at Heroku cannot install dependencies with pip - python

i'm trying to deploy a django project on Heroku. I use virtualenv and pip to manage dependencies. The virtualenv version is 1.7.1 and pip that comes with it it's 1.1. I tried to force the --no-site-packages but it prompts that it's the default behavior now. Also i'm sure that i install everything within the virtual environment. The django project it's empty. I followed the instructions here . Maybe the problem it's that their versions of pip and virtualenv are older? Has anyone encountered this problem? Thank you in advance.
ψ pip.exe freeze
Django==1.3.1
distribute==0.6.24
versiontools==1.8.3
(venv)
ψ more .\requirements.txt
Django==1.3.1
distribute==0.6.24
versiontools==1.8.3
(venv)
ψ git push heroku master
Enter passphrase for key '/c/Program Files (x86)/Vim/.ssh/id_rsa':
Counting objects: 12, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (12/12), 3.30 KiB, done.
Total 12 (delta 2), reused 0 (delta 0)
-----> Heroku receiving push
-----> Python/Django app detected
-----> Preparing virtualenv version 1.7
New python executable in ./bin/python
Installing distribute.............................................................................................................................................................................................done.
Installing pip...............done.
-----> Activating virtualenv
-----> Installing dependencies using pip version 1.0.2
Exception:
Traceback (most recent call last):
File "/tmp/build_k6zl66ydgqxp/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/basecommand.py", line 126, in main
self.run(options, args)
File "/tmp/build_k6zl66ydgqxp/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/commands/install.py", line 200, in run
for req in parse_requirements(filename, finder=finder, options=options):
File "/tmp/build_k6zl66ydgqxp/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/req.py", line 1255, in parse_requirements
req = InstallRequirement.from_line(line, comes_from)
File "/tmp/build_k6zl66ydgqxp/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/req.py", line 82, in from_line
elif os.path.isdir(path) and (os.path.sep in name or name.startswith('.')):
File "/tmp/build_k6zl66ydgqxp/lib/python2.7/genericpath.py", line 41, in isdir
st = os.stat(s)
TypeError: must be encoded string without NULL bytes, not str
Storing complete log in /app/.pip/pip.log
! Heroku push rejected, failed to compile Python/django app

It seems that the problem it is in the requirements.txt, i created it using powershell and "pip.exe freeze > requirements.txt" command,
which creates a file with a name that has nullbytes in it.
I was able to get around this problem by downloading a requirements.txt from an example project on github and modifying it.
That did the job and everything works nice.

Related

Raspberry Pi - Python Deployment - Pipenv

Goal.
I want to build a small weatherstation using a Raspberry Pi 3 B. For measuring temperature and humidity I use an Adafruit DHT22 sensor. There is a Python library to read the sensor.
https://pypi.org/project/Adafruit-DHT/
After making a measurement the measurement data shall be sent to a server.
What I did.
This Adafruit_DHT library is quite picky. For development I had the following setup:
Plan: Develop on a laptop not directly on the Pi. Save everything to Github and then just call git clone ... on Pi to get the code.
Since using pipenv is a recommended workflow I tried to write the code on my laptop and test it using a dummy function for the acutal measurements. That did not work since upon calling pipenv install Adafruit_DHT there was an error because Adafruit_DHT only works on a Pi or an Beaglebone Black.
So, I just wrote the code on my laptop and pushed it to Github without having tested it.
Then I used ssh to connect to the Pi and git clone ... everything.
pipenv install worked.
pipenv run python measurements.py worked too.
Then I wanted to start the script upon startup. I read
https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/
For this I would need to use pipenv run /home/pi/.../main.py, maybe even with superuser privileges.
Encountered Problem.
It does not work since pipenv tries to create a new virtual environment all the time. I did not find anythink like yarn --cwd as in Run yarn in a different path
.
Then I tried pipenv install --system
I got
pipenv install --system
Installing dependencies from Pipfile.lock (dcc369)…
An error occurred while installing adafruit-dht==1.4.0 --hash=sha256:e927f2232eff5335cb9d8a2cca6dcad4625e61f205b12e31ef04198ea6dec830! Will try again.
🐍 β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰ 8/8 β€” 00:00:16
Installing initially failed dependencies…
[pipenv.exceptions.InstallError]: File "/home/pi/.local/lib/python3.7/site-packages/pipenv/core.py", line 1874, in do_install
[pipenv.exceptions.InstallError]: keep_outdated=keep_outdated
[pipenv.exceptions.InstallError]: File "/home/pi/.local/lib/python3.7/site-packages/pipenv/core.py", line 1253, in do_init
[pipenv.exceptions.InstallError]: pypi_mirror=pypi_mirror,
[pipenv.exceptions.InstallError]: File "/home/pi/.local/lib/python3.7/site-packages/pipenv/core.py", line 859, in do_install_dependencies
[pipenv.exceptions.InstallError]: retry_list, procs, failed_deps_queue, requirements_dir, **install_kwargs
[pipenv.exceptions.InstallError]: File "/home/pi/.local/lib/python3.7/site-packages/pipenv/core.py", line 763, in batch_install
[pipenv.exceptions.InstallError]: _cleanup_procs(procs, not blocking, failed_deps_queue, retry=retry)
[pipenv.exceptions.InstallError]: File "/home/pi/.local/lib/python3.7/site-packages/pipenv/core.py", line 681, in _cleanup_procs
[pipenv.exceptions.InstallError]: raise exceptions.InstallError(c.dep.name, extra=err_lines)
[pipenv.exceptions.InstallError]: ['Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple', 'Collecting adafruit-dht==1.4.0 (from -r /tmp/pipenv-m9p05m29-requirements/pipenv-uuyle1le-requirement.txt (line 1))', ' Using cached https://www.piwheels.org/simple/adafruit-dht/Adafruit_DHT-1.4.0-cp37-cp37m-linux_armv7l.whl']
[pipenv.exceptions.InstallError]: ['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.', ' adafruit-dht==1.4.0 from https://www.piwheels.org/simple/adafruit-dht/Adafruit_DHT-1.4.0-cp37-cp37m-linux_armv7l.whl#sha256=3a47d226e77186f89bf167a6568d9ae2cab119333c4b3f5a8ec460f9695a832c (from -r /tmp/pipenv-m9p05m29-requirements/pipenv-uuyle1le-requirement.txt (line 1)):', ' Expected sha256 e927f2232eff5335cb9d8a2cca6dcad4625e61f205b12e31ef04198ea6dec830', ' Got 3a47d226e77186f89bf167a6568d9ae2cab119333c4b3f5a8ec460f9695a832c']
ERROR: ERROR: Package installation failed...
Some how the hash changed. How can that happen? (btw, I ran pipenv install just 1 minute before pipenv install --system).
Questions.
What would be a recommended way to run this program on startup?
My main problem is not the problem itself. Maybe, I could get it to work without pip or pipenv. Nevertheless, I like sandboxing. In C++ for example one would compile it -> just one executable.
What about freezing the program with e.g. pyInstaller? How to deal with pipenv and pyInstaller at the same time.
I am writing this post, since I have already encountered much simpler workflows like Javascript + nodeJs + yarn.
I'd appreciate any help.
For question 1 there's couple ideas discussed in Issue on pipenv's Github: recommended method of use in crontabs? Β· Issue #1369
Suggested option for activating the correct env is to cd into the directory before using pipenv run, something like
cd /home/pi/.../ && pipenv run main.py
Other options are to activate the virtual env without pipenv
source /path/to/virtualenv/activate && python /home/pi/.../main.py
or point directly to the Python inside virtualenv
#!/bin/sh
VENV_PYTHON="/path/to/project-hAsHpTH/bin/python"
PROJECT="/path/to/some/project"
SCRIPT="script.py"
cd "${PROJECT}" && "${VENV_PYTHON}" "${SCRIPT}"

Flask app not compatible with Buildpack Python heroku

I have Flask application that I want to apply to Heroku.
I have made a Procfile as such:
web: gunicorn routes:app
and a requirements.txt:
click==6.7
Flask==0.12
gunicorn==19.6.0
itsdangerous==0.24
Jinja2==2.8.1
MarkupSafe==1.0
Werkzeug==0.14.1
but whenever I try to run the command:
git push heroku master
I always get this error:`App not compatible with buildpack: https://buildpack-registry.s3.amazonaws.com/buildpacks/heroku/python.tgz
even though I have set the buildpack to python. My main python file is called routes.py, so the Profile should be correct, and I have made a lot of research and all the dependencies seems to be there, what could be the issue?
for all my dependencies I also have a Pipfile and a Pipfile.lock..
I tried using pip install --upgrade -r requirements.txt
and this resulted in this error:
No matching distribution found for adium-theme-ubuntu==0.3.4 (from -r /tmp/build_622384b275f7a5f640333152a3b25ba1/requirements.txt (line 1))
output of Git Status:
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
Errors i get from my requirements.txt
File "/app/.heroku/python/lib/python3.6/site-packages/setuptools/__init__.py", line 5, in <module>
import distutils.core
File "/app/.heroku/python/lib/python3.6/distutils/core.py", line 16, in <module>
from distutils.dist import Distribution
File "/app/.heroku/python/lib/python3.6/distutils/dist.py", line 9, in <module>
import re
File "/app/.heroku/python/lib/python3.6/re.py", line 142, in <module>
class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-oqscorl3/enum34/
Push rejected, failed to compile Python app.
When I recently received similar errors, I was able to drop the version numbers from my requirements.txt to get a successful deploy.
For anyone still seeking an answer, it appears the requirements.txt is missing dependency adium-theme-ubuntu==0.3.4 as stated in the error message. The package doesn't exist on PyPI, so pip install won't work. That's why the deployment failed.
In this case, it's often helpful to run pip freeze again to see if any new dependency caused error.
If nothing helps, better do a clean reinstall of everything:
rm -rf venv
python -m venv venv
pip install DEPENDENCY-NAME
Then try deploying again.

Heroku warning message about setuptools/distribute in requirements.txt

Pushing a python app to Heroku, I'm getting this:
! The package setuptools/distribute is listed in requirements.txt.
! Please remove to ensure expected behavior.
The docs explicitly include distribute in their requirements.txt files, and when I remove it, I get this (which sure seems to depend on distribute):
Traceback (most recent call last):
File "/app/.heroku/python/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
! Push rejected, failed to compile Python app
This question is crazy old, but for anyone who stumbles upon this and still needs an answer, you should try upgrading your Heroku buildpack (the collection of scripts that build your app on the dynos). This will update pip (among other things), since you can't exactly do what Mauro suggested because the code is being deployed to Heroku dynos.
More info on buildpacks: https://devcenter.heroku.com/articles/buildpacks
But if you don't want to read the article, just do
$ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-python
Then re-attempt to deploy your app like normal.
Upgrade setuptools and distribute.
pip install --upgrade setuptools
pip install --upgrade distribute
Update your requirements.txt. This worked for me exactly yesterday.

Can't install NLTK with setup.py

I have installed python 2.7 and am trying to set up nltk to do some data analysis (I'm fairly new to the whole programming game). However, I can't seem to install nltk. I downloaded setuptools but it keeps telling me that it can't be found. Do any of you python masters know how to fix this? :)
$ python ez_setup.py
ez_setup.py:129: Warning: 'as' will become a reserved keyword in Python 2.6
File "ez_setup.py", line 129
except pkg_resources.VersionConflict as VC_err:
^
SyntaxError: invalid syntax
$ sudo git clone https://github.com/nltk/nltk.git
Password:
Cloning into 'nltk'...
remote: Reusing existing pack: 48415, done.
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 48422 (delta 0), reused 2 (delta 0)
Receiving objects: 100% (48422/48422), 65.84 MiB | 1.78 MiB/s, done.
Resolving deltas: 100% (32659/32659), done.
Checking connectivity... done
$ cd nltk
$ python setup.py install
Traceback (most recent call last):
File "setup.py", line 33, in <module>
from setuptools import setup, find_packages
ImportError: No module named setuptools
It seems you do not have python2.7-dev is not installed.
Try installing python2.7-dev as below and try running setup again.
$ sudo apt-get install python2.7-dev
$ sudo python setup.py install

Heroku can't find pip (very strange)

When I push to master, this happened:
Counting objects: 1, done.
Writing objects: 100% (1/1), 186 bytes, done.
Total 1 (delta 0), reused 0 (delta 0)
-----> Fetching custom git buildpack... done
-----> Python app detected
-----> Preparing Python runtime (python-2.7.3)
-----> Installing Distribute (0.6.35)
-----> Installing Pip (1.3.1)
-----> Installing dependencies using Pip (1.3.1)
/tmp/buildpack_12iaablbmqzbb/bin/compile: line 164: /app/.heroku/python/bin/pip: No such file or directory
! Heroku push rejected, failed to compile Python app
To git#heroku.com:xxxxxxxxxx.git
! [remote rejected] master -> master (pre-receive hook declined)
To be clear, I specified build pack, because heroku wrongly detected my python app as ruby app due to the existence of Gemfile.
This is the config I pulled from heroku config
BUILDPACK_URL: https://github.com/heroku/heroku-buildpack-python
But this fail to compile my app, how can I really solve this?
Thanks.
The problem is pip cannot find the dependencies. You can do is,
virtualenv --no-site-packages venv
virtualenv --no-site-packages and pip still finding global packages?

Categories

Resources