Docker not recognising psycopg2-binary as psycopg2 - python

Using pipenv I have the following pipfile:
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
flask = "*"
uuid = "*"
gunicorn = "*"
psycopg2-binary = "*"
[dev-packages]
[requires]
python_version = "3.8"
And then my Dockerfile is set up as follows:
FROM python:3.8.3-slim-buster
RUN useradd deploy_trial
WORKDIR /home/deploy_trial
RUN pip install pipenv
COPY . /home/deploy_trial/
RUN pipenv install --deploy
CMD ["python","./app/text.py"]
However, although the build seems to go successfully, when I run the image I get an error message saying:
Traceback (most recent call last):
File "./app/text.py", line 1, in <module>
import psycopg2
ModuleNotFoundError: No module named 'psycopg2'
So it obviously thinks that psycopg2 hasn't been installed... This is really strange because when I have used psycopg2-binary on my local machine and all of my programs there recognise the installation as psycopg2 when the code is run. Does anyone know how to fix this?

My dockerfile:
FROM python:3.8.3-slim-buster
RUN python -m pip install pipenv
COPY script.py script.py
COPY Pipfile Pipfile
RUN pipenv install
CMD ["pipenv", "run", "python", "script.py"]
script.py:
import psycopg2
print('it is ok')
Pipfile:
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
psycopg2-binary = "*"
[dev-packages]
[requires]
python_version = "3.8"
It works with such configuration.

Related

How to install Python packes listed in `Pipfile` using `pipenv` and the `--target` flag (via `--extra-pip-args`)?

A) The following does not install the packages from Pipfile into the --target directory foo:
pipenv install --extra-pip-args="--target=foo"
B) On the other hand, this does install the explicitly specified package(s) (request in this case) into the --target directory foo:
pipenv install --extra-pip-args="--target=foo" requests
How can I make the first command work? It would be preferable, as Pipfile already has version-pinned packages listed:
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
requests = "2.28.2"
[dev-packages]
[requires]
python_version = "3.9"

Install package with Pipenv

I'm trying to use cryptography package in Python in virenv but VsCode error is " no module with this name : Crypt.py(script) and Pipfile are in the same directory
...my direcotry > pipenv install cryptography
....>
crypt.py
from cryptography.fernet import Fernet
key = Fernet.generate_key()
print(key)
error :
from cryptography.fernet import Fernet
ModuleNotFoundError: No module named 'cryptography'
pipfile:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
cryptography = "*"
[requires]
python_version = "3.8"
Make sure you're running python within the virtual env you set up with Pipenv:
pipenv run python my_script.py

Installing TensorFlow with pipenv gives error

I'm trying to install TensorFlow using pipenv.
This is my Pipfile:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
pylint = "*"
[packages]
python-telegram-bot = "*"
imdbpy = "*"
matplotlib = "*"
scikit-image = "*"
scikit-learn = "*"
tensorflow = "*"
[requires]
python_version = "3.8"
I then run:
pipenv install tensorflow
Which outputs:
Installing tensorflow…
Adding tensorflow to Pipfile's [packages]…
Installation Succeeded
Pipfile.lock (989c3d) out of date, updating to (0d6760)…
Locking [dev-packages] dependencies…
Success!
Locking [packages] dependencies…
Locking Failed!
Followed by a big traceback that ends with:
pipenv.patched.notpip._internal.exceptions.InstallationError: Command "python setup.py egg_info" failed with error code 1 in C:\Users\lucas\AppData\Local\Temp\tmpyh639mq4build\functools32\
My virtual environment uses Python 3.8.0 64 bit.
What am I doing wrong?
As the comments pointed out, Tensorflow only supports up to python 3.7 (as March 2020). You can find more info in the system requirements page of the documentation.
So, to fix your issue:
Remove the virtual environment with pipenv --rm
Remove the Pipfile.lock
Change the last lines of your Pipfile to
[requires]
python_version = "3.7"
Run pipenv install --dev to recreate the environment again and pipenv install tensorflow to install tensorflow
Done!

Question about 2 instances of package in pipfile

Im going through exercises on https://www.practicepython.org/
In ex17 there is a task to install packages "request" and "BeautifulSoup"
I did it but since an error occured i installed "BeautifulSoup" and "BeautifulSoup4"
Pipfile:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
requests = "*"
beautifullsoup = "*"
beautifulsoup4 = "*"
[requires]
python_version = "3.7"
Will it couse any problems if i will import only beautifulsoup4?
Can I delete the beautifullsoup = "*" from file and it will be ok?
How can i do it?
Should i write additional commands in console?
Just run pipenv uninstall beautifullsoup to remove the installed package and you should be fine.

python pip: cannot install flask and zappa - contradicting requirements

I want to install flask and zappa, a common combination.
I use pipenv to create an evironment:
pipenv --python 3.6.4
I want to install packages:
pipenv install flask zappa
Unfortunately the requirements of flask is:
Werkzeug >= 0.14
the requirement of zappa is :
Werkzeug == 0.12
So this is not installable. What can i do?
Thanks to Evgeny this solution was successful. I could not find it in pipenv documentation.
Just manually edit the Pipfile:
Pipfile
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"
[dev-packages]
[packages]
zappa = { git = 'https://github.com/Miserlou/Zappa.git', ref = 'master', editable = true }
flask = "*"
[requires]
python_version = "3.6"

Categories