How to recreate a virtual env in python - 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.

Related

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

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

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

How to setup virtual environment for python(2.7, 3.5, 3.6) on Ubuntu16.04 LTS?

I've read a lot of blog post about this and I'm still confused as to what is "best" way to set it up. Most of the blog posts I've read are out-dated. I'm new to Linux and have messed up my system twice now and still can't setup the virtual environments properly. According to what I've read, Virtualenv and Virtualenvwrapper combination is the most widely used setup. So, After a fresh Ubuntu 16.04 LTS install, I do the following:
Install Python 3.6 as shown in the following link.
https://tecadmin.net/install-python-3-6-ubuntu-linuxmint/
The current state of the system now is,
$ python3.6 -V
Python 3.6.4
$ pip3.6 -V
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)
pip3 (python3.5) and pip(python2.7) do not come pre-installed. To install them , I do:
$ sudo apt-get install python-pip
$ sudo apt-get install python3-pip
$ which pip
/usr/bin/pip
$ which pip3
/usr/bin/pip3
Now, the pip-version installed through apt-get method is old(version 8.1.1). We need to update it to (version 9.0.1). This is where it all goes wrong.
Question 1: How do I update the two different pip versions without breaking anything?
Assume, both pip versions are upgraded to version 9.0.1
Now, I have to install virtualenv and virtualenvwrapper.
Which pip version do I use to install it?
$ pip install --user virtualenv and $ pip install --user virtualenvwrapper
or
$ pip3 install --user virtualenv and $ pip3 install --user virtualenvwrapper
ps: I'm following this link-
http://chrisstrelioff.ws/sandbox/2016/09/21/python_setup_on_ubuntu_16_04.html
With python 3.6 virtual environments come built-in with the venv module:
python3.6 -m venv my-venv
To create a virtual environment for python 3.5:
virtualenv -p python3.5 env
To create a virtual environment for python 2.7:
virtualenv -p python2.7 env
Try using conda to set up virtual environments ?
With conda you can create a virtual environment and keep each of them separate from the root environment.

Ubuntu says virtualenv is not installed but pip says it is

I am trying to create another virtual environment (I already installed one using the typical instructions found here: http://docs.python-guide.org/en/latest/dev/virtualenvs/) so I run:
$ virtualenv experimental
-> The program 'virtualenv' is currently not installed. You can install it by typing: sudo apt install virtualenv
I checked to see if perhaps the program needed to be updated:
$ pip install virtualenv --upgrade
-> Requirement already up-to-date: virtualenv in /home/uniside/.local/lib/python2.7/site-packages
Any ideas about what is going on here?
Use sudo. Currently it's being install in your local directory.
sudo pip install virtualenv
The answer by #khrm didn't worked for me.
I was able to do this with :
sudo apt install virtualenv
I had problems activating my virtualenv projects with Ubuntu's system version, so I just use pip's virtualenv. Works with python2 as well:
$ pip3 install virtualenv
Just call virutalenv via python3 -m:
$ python3 -m virtualenv --help
Usage: virtualenv.py [OPTIONS] DEST_DIR

How to install pip with two python version exist on CentOS 6.4?

I have a CentOS 6.4 which the default python version is 2.6.
I want to run a virtualenv at python 2.7, so first I try to install python 2.7.
yum install python27
Then I run
virtualenv -p /usr/bin/python2.7 ./venv
Then the output shows that it try to get setuptools from pypi, but my environment can not reach pypi.python.org. I have update the ~/.pip/pip.conf to use a available local source but the virtualenv still get pip from the pypi.python.org. This is one of the things I get confused.
I check the /usr/lib/python2.7/, the site-packages is empty while /usr/lib/python2.6/ is not. So When I use python 2.7, it has nothing available. When I use the default python, it has pip tools installed, it does not need to get it from pypi.python.org.
How can I install pip for python 2.7 individually?
Previous I install pip by
yum install python-setuptools
yum install python-pip
The second question, how to install pip for python2.7 individually can be solved as the following:
Download python source code of the specific version and install it at /usr/local
wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
tar xf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
Then install setuptools and pip
Install easy_install (prepare ez_setup.py beforehand)
python2.7 ez_setup.py
Install pip
easy_install-2.7 pip
easy_intall-2.7 pip need to visit the internet, in my environment I get the pip source file and use "python2.7 setup.py install".
pip2.7 install [packagename]
In my environment I update .pip/pip.conf to use an internal pip source.
(code above take python 2.7 as an example)
To see the details you can check the following url:
http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/
The first question, when creating a new virtual environment, virtualenv will install setuptools for this environment. If python version is not assigned, it use the system default python and default pip. If pip is not find, then try to get it from pypi.python.org.
In my environment, after python2.7 and pip2.7 installed, when trying to create a new virtualenv, it still get pip from pypi.python.org, I guess that virtualenv does not find the relation between python2.7 and pip2.7.
If you can visit pypi.python.org, that is fine.
If you are at an internal environment that can not visit pypi.python.org, virtualenv provides -extra-search-dir and --never-download command. So you can prepare the setuptools beforehand, copy it from U-diskļ¼Œusing scp, or other solutions.
Move setuptools-0.6c11-py2.7.egg to /usr/local/bin.
Finally we can use virtualenv by
virtualenv -p /usr/local/bin/python2.7 --extra-search-dir /usr/local/bin/ --never-download venv

Categories