How can I upgrade pip inside a venv inside a Dockerfile? - python

While running
$ sudo docker build -t myproj:tag .
I am hit with the message
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
and given recent occasional subtleties manifesting themselves with the error:
"/usr/bin/pip" "from pip import main" "ImportError: cannot import .."
I'd rather yield and indeed upgrade.
And so I add the pip upgrade command in the DockerFile, after the venv is built, since the pip that matters is the one inside the venv (am I getting this right?). So my Dockerfile now has this:
...
RUN python -m venv venv
RUN pip install --upgrade pip
...
But doing so does not avoid the "You are using pip 10.x" message. What am I missing?
Update
Though a promising suggestion, neither
RUN source venv/bin/activate
RUN pip install --upgrade pip
nor
RUN source venv/bin/activate
RUN python -m pip install --upgrade pip
eliminate the "You are using pip version 10.0.1, ..." message.

The single easiest answer to this is to just not bother with a virtual environment in a Docker image. A virtual environment gives you an isolated filesystem space with a private set of Python packages that don't conflict with the system install, but so does a Docker image. You can just use the system pip in a Docker image and it will be fine.
FROM python:3.7
RUN pip install --upgrade pip
WORKDIR /usr/src/app
COPY . .
RUN pip install .
CMD ["myscript"]
If you really want a virtual environment, you either need to specifically run the wrapper scripts from the virtual environment's path
RUN python -m venv venv
RUN venv/bin/pip install --upgrade pip
or run the virtual environment "activate" script on every RUN command; the environment variables it sets won't carry over from one step to another. (Each RUN command in effect does its own docker run; docker commit sequence under the hood and will launch a new shell in a new container; the Dockerfile reference describes this a little bit.)
RUN python -m venv venv
RUN . venv/bin/activate \
&& pip install --upgrade pip
COPY . .
RUN . venv/bin/activate \
&& pip install .
CMD ["venv/bin/myscript"]
Trying to activate the virtual environment in its own RUN instruction does nothing beyond generate a no-op layer.
# This step does nothing
RUN . venv/bin/activate
# And therefore this upgrades the system pip
RUN pip install --upgrade pip

Before you can use your virtual environment venvyou need to activate it with
On Windows:
venv\Scripts\activate.bat
On Unix or MacOS, run:
source venv/bin/activate
Please note that venv is the name of your environment. You created this environment with RUN python -m venv venv. I strongly recommend to use a other name.
Then you can upgrade with python -m pip install --upgrade pip

After you create a virtual environment in a Docker container through
RUN python -m venv venv
then run either
RUN venv/bin/pip install --upgrade pip
or
RUN venv/bin/python -m pip install --upgrade pip
but neither
RUN pip install --upgrade pip
nor
RUN python -m pip install --upgrade pip

Related

How do I install tensorflow in a Docker image w/ venv?

I have the following code...
FROM python:latest
ENV VIRTUAL_ENV "/venv"
RUN python -m venv $VIRTUAL_ENV
ENV PATH "$VIRTUAL_ENV/bin:$PATH"
# Python commands run inside the virtual environment
RUN /venv/bin/python3 -m pip install --upgrade pip
RUN /venv/bin/pip3 install tensorflow
But when I run I get...
ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: No matching distribution found for tensorflow
I tried using the tensorflow image like...
FROM tensorflow/tensorflow:latest
ENV VIRTUAL_ENV "/venv"
RUN python -m venv $VIRTUAL_ENV
ENV PATH "$VIRTUAL_ENV/bin:$PATH"
# Python commands run inside the virtual environment
RUN /venv/bin/python3 -m pip install --upgrade pip
but then I get...
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
So I change to
FROM tensorflow/tensorflow:latest
RUN apt-get install python3-venv -y
ENV VIRTUAL_ENV "/venv"
RUN python -m venv $VIRTUAL_ENV
ENV PATH "$VIRTUAL_ENV/bin:$PATH"
# Python commands run inside the virtual environment
RUN /venv/bin/python3 -m pip install --upgrade pip
But I get...
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/p/python3.6/python3.6-venv_3.6.9-1~18.04ubuntu1.1_amd64.deb 404 Not Found [IP: 91.189.88.152 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
How do I handle this?
Per #drum comment this works...
FROM tensorflow/tensorflow:latest
RUN apt-get update && apt-get upgrade -y
RUN apt-get install python3-venv -y
ENV VIRTUAL_ENV "/venv"
RUN python -m venv $VIRTUAL_ENV
ENV PATH "$VIRTUAL_ENV/bin:$PATH"
# Python commands run inside the virtual environment
RUN /venv/bin/python3 -m pip install --upgrade pip

ModuleNotFoundError: No module named 'virtualenv.seed.embed.via_app_data' when I created new env by virtualenv

I was creating a new virtual environment on Ubuntu 20.04:
$ virtualenv my_env
But it gave an error:
ModuleNotFoundError: No module named 'virtualenv.seed.embed.via_app_data'
Other info:
$ virtualenv --version
virtualenv 20.0.17 from /usr/lib/python3/dist-packages/virtualenv/__init__.py
#yushulx
I also ran into the same issue. I installed both via pip3 and via sudo apt install python3-virtualenv and it gave me an error but after I ran pip3 uninstall virtualenv I could create a virtualenv without issue
Try to create the virtual environment using directly venv module
python3 -m venv my_env
To fix this on Ubuntu 20.04, I had to uninstall virtualenv from the system: apt remove python3-virtualenv, and reinstall it using pip: pip install --user virtualenv --force-reinstall. I had errors about dependencies conflicts, I fixed them by calling pip install --user ${package} --force-reinstall for every package involved.
virtualenv is installed by default with python itself and when you install virtualenv via pip3 and try to create virtual environment using pipenv you will get this error:
ModuleNotFoundError: No module named 'virtualenv.seed.embed.via_app_data
check the version of installed virtualenv using apt list --installed
mine is:
python3-virtualenv/focal,focal,now 20.0.17-1 all [installed,automatic]
with the installed virtualenv by pip3
min is :
virtualenv 20.4.0
default installation of virtualenv is different with pip3 installed virtualenv
so when you try to create a virtual environment using pipenv for example installing django in a directory home/user/djano with pipenv install django~=3.1.5 you will get that error
the solution is remove installed virtualenv using pip3 uninstall virtualenv and use the default installation of virtualenv this time when you create virtual environment with pipenv it will create it successfully.
I want to have virtualenvwrapper. On Debian 10 testing I did:
apt remove python3-virtualenvwrapper # not purge, I want no changes in ~/.virtualenvs/
apt purge python3-virtualenv
/usr/bin/python3.8 -m pip install --force-reinstall virtualenvwrapper
/usr/bin/python3.8 -m pip install --force-reinstall virtualenv==20.0.23
.24 no longer works. I hope it will be solved sometimes...
EDIT 2021.01: I have changed my stack to: pyenv + pyenv-virtualenvwrapper + poetry. Ie. I use no apt or pip installation of virtualenv or virtualenvwrapper, and instead I install pyenv's plugin pyenv-virtualenvwrapper. This is easier way.
If someone encounters this problem inside existing env (when for example using pyenv) you can also use command below (found on GitHub when tried to fix poetry virtual env installation):
pip install --force-reinstall virtualenv
When I installed virtualenv via pip3, it failed to run virtualenv command. Then I changed the installation via:
sudo apt install python3-virtualenv
The virtualenv command can normally work.
I too had this issue. What I found is it is a permissions issue. For some unknown reason ownership of my home directory was off. I did a chown -R for the directory I was using for my project making myself the owner of my own directory and now everything works as normal.
I also had same issue, seems installed version has different user level so I followed their doc and below one work for me:
python3 -m virtualenv --help
To create new environment:
python3 -m virtualenv my_env
I also faced the same issue but after removing virtualenv which was installed with pip3, I could get rid of this error. Uninstall virtualenv with below command (don't forget to use sudo)
sudo pip3 uninstall virtualenv
After this, virtualenv command works totally fine.
It means that there are two virtualenv in your system.
One is "pip install" by sudo or root, the other may be installed by apt(if you are using ubuntu os)
Just uninstall one of them and the error should be fixed.
I fixed this error by removing all virtualenv and virtualenvwrapper related packages on system and reinstall the virtualenv and virtualenvwrapper with pip with below command(as i use ubuntu, so below only show apt)
remove all packages shown in below result
apt list --installed | grep virtualenvwrapper
apt list --installed | grep virtualenvwrapper
install virtualenv virtualenvwrapper with pip
pip install virtualenvwrapper virtualenvwrapper
set ~/.zshrc
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/amd
export VIRTUALENVWRAPPER_SCRIPT=/home/robot/.local/bin/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
source /home/robot/.local/bin/virtualenvwrapper.sh
When we use pip3 or python3 to install virtualenv then I got that error too. I had to run each time to create virtualenv (my_env is virtual environment name)
python3 -m virtualenv my_env
But if I install it using
sudo apt install virtualenv
Then virtualenv command works fine.
virtualenv my_env

Python not seeing packages after pip install

When I run the following command within my virtual env
sudo pip3 install -r requirements.txt
It says that the packages were successfully installed, but when I try to run or import the packages, it can not find them.
pip3 show returns nothing.
However, when I manually run
sudo pip3 install package-name
It installs the package just fine and it works.
Why is pip install -r requirements.txt not working? It always worked in the past. Now that I reinstalled Python it stopped working..
System:
Ubuntu 14.04
Python changed from 3.4 to 3.6.2
requirements.txt
Django==2.0.8
django-debug-toolbar
channels
Debugging in Terminal:
EDIT: This makes no sense.
pip3 install -r requirements.txt
Requirement already satisfied: pycparser in /usr/local/lib/python3.6/site-packages (from cffi!=1.11.3,>=1.8->cryptography>=2.7->autobahn>=0.18->daphne~=2.3->channels==2.3.0->-r requirements.txt (line 79)) (2.19)
$ pip3 --version
pip 19.2.3 from /home/dominic/Desktop/projects/printrender/env/lib/python3.6/site-packages/pip (python 3.6)
I install packages in my Virtual Environemnt using pip3 install -r requirements and it says that they are already installed, but when I run Pip Freeze, it returns nothing, as if nothing is installed.
Pip3 install -r requirements is placing my packages in my local packages python packages, and pip freeze is referencing my virtual env packages.
pip is not installing this packages in the correct place
I don't think you should use sudo when you're using a virtual environment. Try without.
I think you created a virtual environment for python 2 by mistake since pip3 is used from /usr/local/lib/python3.6 instead of in the env. You can create the virtual environment specifically for python3 by using the command
virtualenv -p python3 env
Could you try creating a new virtual environment with the command above and see if it works?
Using sudo was part of the issue and some of the packages in my requirements.txt were causing errors with the latest version of pip.
When you use sudo, you installed your packages globally. This must solve your problem.
sudo su
. venv/bin/activate
pip install -r requirements.txt

How to recreate a virtual env in python

I have installed virtualenv and virtualenvwrapper on Ubuntu 16.04
I have created one enviroment named env1
$ sudo apt-get install python-pip
$ pip install virtualenv
$ pip install --upgrade pip
$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/virtualenvwrapper.sh
$ mkvirtualenv env1
Once in (env1) I have installed several packages
(env1) $ pip install numpy
(env1) $ pip install scipy
(env1) $ pip install matplotlib
(env1) $ apt-get install python-tk
I have also installed opencv3 (I am not copying how because is too long)
I am using env1 for a specific project.
Now I want to start another project using the same packages, but I also want to add other packages.
I have created env2, and I was wondering if it is possible to copy env1 to env2 without the need to re-install everything again from scratch.
Your best option is to do this:
virtualenv-1:
pip freeze > requirements.txt
virtualenv-2:
pip install -r requirements.txt
Assuming they are both the on the same system and use the same Python, it is somewhat possible to just copy the site-packages:
cp -Rp /environments/virtualenv-1/lib/python2.7/site-packages \
/environments/virtualenv-2/lib/python2.7/site-packages
This won't necessarily work though:
some packages will install dependencies and other things into /bin/ or elsewhere. most don't, but many do.
if the python versions for the virtualenvs differ -- even on a minor version -- that can break libraries that use c extensions.
So your best bet is to pip freeze and reinstall from that file.
pip install virtualenvwrapper and use the cpvirtualenv command
cpvirtualenv ENVNAME [TARGETENVNAME]
http://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html#cpvirtualenv
Remember to heed the warning
Copying virtual environments is not well supported. Each virtualenv has
path information hard-coded into it, and there may be cases where the copy
code does not know to update a particular file. Use with caution.

Why is python saying I have "no module named venv"?

I installed virtual env with sudo pip install virtualenv but when I run python -m venv flask I'm still getting this: /usr/bin/python: No module named venv
Versions, if that's relevant:
pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
Python 2.7.9
what am I missing here?
Since you are on Python 2, you need to execute using the virtualenv module that you installed.
First step, as you originally tried to do, but this time you specify the "virtualenv" module and the name of the virtualenv. In this case flask:
python -m virtualenv flask
Then you activate your virtualenv like this:
source flask/bin/activate
Then install flask with pip inside the virtualenv
pip install flask
If you want to deactivate your virtualenv, simply type:
deactivate
If running on Python 3, the venv command is built-in and you can simply do:
python3 -m venv flask
Note, depending on how your Python 3 is installed, your python execution command might differ. You could be running it as python3, python3.5, python3.6.
venv is a module introduced in python3
venv is New in version 3.3.
The venv is ony available in python 3 version. If you are using python 2 then try to use virtualenv instead of venv.
1. Install virtualenv,
python -m pip install virtualenv
2. Create a virtual environment named venv using virtualenv,
Python 2
python -m virtualenv venv
Python3
python -m venv venv
3. Activate virtual environment,
.\venv\Scripts\activate.bat
4. Install flask package,
pip install flask
If are you using "windows".
Try it in "cmd"
navigate in cmd to the folder you want to install venv and do:
python3 -m venv project_env
You can change the name of the project to.
I changed python -> python3:
python3 -m venv flask
For python3 users, just be sure that you installed pip and venv packages:
sudo apt install python3-pip
sudo apt install python3-venv
Then you can use it like this:
python3 -m venv ~/sample
. ~/sample/bin/activate
pip install flask
Do the following for this issue.
pip install venv
(If this has got some issue that means python version recognized by your machine don't have updated version) so use below command:
pip install the virtualenv
python -m venv <>
if this has got the same issue so use below one.
python -m virtualenv <>
sudo apt-get install python3-pip
python3 -m pip install virtualenv
python3 -m virtualenv venv
source venv/bin/activate

Categories