简体   繁体   中英

Deploying Flask application to Heroku gives an ImportError from psycopg2

Recently I'm learning Flask by the book "Flask Web Development". When I completed the code and deployed it to Heroku, the following error happened:

ImportError: /app/.heroku/python/lib/python3.6/
sitepackages/psycopg2/.libs/libresolv-2-c4c53def.5.so: 
symbol __res_maybe_init version GLIBC_PRIVATE not defined 
in file libc.so.6 with link time reference

However, this works fine locally. I have searched relevant questions about psycopg2 , and I have adjusted the version of psycopg2 but the same error still happens. Please, how can I solve the problem?

I got the same problem. I solved it by forcing heroku to clean up the python virtual environment, and reinstall the requirements.txt file using psycopg2>=2.7,<2.8 --no-binary psycopg2 . I must admit it felt a little like magic, but for what its worth here are the steps I took:

  1. Using Git Bash for Windows, I logged in to heroku with heroku login .
  2. Next, I navigated to the root folder of the git/project folder I was working on.
  3. There I made sure that my requirements.txt was updated by running venv/Scripts/pip freeze > requirements.txt . You will probably have to find your own python/virtual environment path.
  4. Next I changed the line/entry for psycopg2 to psycopg2>=2.7,<2.8 --no-binary psycopg2 .
  5. Then saved all the content of the requirements.txt file to my desktop, making sure the one in my project folder was empty. Not sure if this step was necessary. But that's what I did, so I'm just gonna mention it here.
  6. Now in the Git Bash terminal I ran heroku apps:info -a blogglistene to find the version of the my heroku stack which for me was heroku-18 .
  7. Next I look online for the supported versions of python for heroku-18 . It said the default was python-3.6.6 (which I then assumed is the one I was running), and some others.
  8. I then picked python-2.7.15 arbitrarily from the list, and added that text to a runtime.txt file which I placed in the root of my project folder.
  9. Next I committed and pushed this to my heroko git repo, and it was deployed. This will temporarily break the build. From what I understand, the magic behind this is that heroku now fully destroys the python interpreted and environment, so no lingering cached files or anything. Clean slate.
  10. Then I reverted the content of runtime.txt to the default of python-3.6.6 and added back the content of the requirements.txt file, making sure the changes I made are there.
  11. Finally, I committed and pushed this, and my application was back up and running.

I had the exact same problem when I was following "Flask Web Development". I spent a day plus trying to solve it and eventually succeeded. It's not as complicated as André C. Andersen's method.

  1. Make sure you are on master branch. Not other branches.

The book is second edition of Flask Web Development by Miguel Grinberg. Chapter 17. Deployment. When you work all the way till Deploying with git push , after git push heroku master to upload the application to the heroku remote. The application is now deployed and running, but it is not going to work correctly because the deploy command that initializes the database tables has not been executed yet. You need to use

heroku run flask deploy

to create database on heroku. That's where the problem appears.

This is because at this point your git repo HEAD is at 17c. It's not the master branch yet. If you commit any change now, it won't change heroku environment. Meaning when you do

git branch

You will see:

(HEAD detached from 17c)
  master

But this is not what you want. Not head attached to 17c. You want to see only master , which is achieved with

git checkout master

Then git branch will show only master branch. Therefore you can make adjustments on the project and heroku will update. Doing this because you will eventually git push to master branch. I think you can git push to other branches in order for heroku to work, but I don't know how to do that.

  1. Change psycopg2==2.7.3 to the newest version, eg psycopg2==2.8.4 . Another workaround is to change it to psycopg2-binary==2.8.3 .

psycopg2 2.7.3 will give you GLIBC_PRIVATE error. (You can check it by running heroku run bash , then starting python and importing psycopg2. If heroku dependency is 2.7.3, then you will see the same error.) The official website notes that version 2.7.3.1 drops libresolv which causes the issue, therefore using this version or later should do the trick.

Now, change your requirements/heroku.txt (heroku environment dependency) line psycopg2==2.7.3 to psycopg2--2.8.4 or psycopg2-binary==2.8.3 . (For some reason binary version of psycopg2 will work without issue. Don't know why, but I saw A LOT of discussion on stackover.) Then,

git add . git commit -m "notes"

As usual.

git push heroku master

Push to heroku master branch. If you do not do step 1, then git will show Everything up-to-date . Therefore, it won't make changes because you are committing not to master. Now, you can see git is downloading the appropriate package.

You can double check if the packages are correct by opening heroku bash then pip list . You will see psycopg2 (and psycopg2-binary if you chose the workaround). Now, when you start Python, you can import psycopg2 with no problem.

Finally. heroku run flask deploy . Voila!

I was stuck on step 1, since whatever change I made with the project, heroku just didn't update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM