How to freeze packages installed only in the virtual environment, that is, without the global ones?
You need to use the -l or --local option to freeze only the local packages (and not the global ones)
pip freeze -l > requirements.txt
Make sure you are working in the virtualenv before doing pip freeze -l.
Only local packages on virtual environment
pip freeze -l > requirements.txt # or --local instead of -l
Only local packages installed by the user on virtual environment
pip freeze --user > requirements.txt
See the documentation for further details: https://pip.pypa.io/en/stable/reference/pip_freeze/.
For me (macOS) the following worked
path/to/venv/bin/pip3 freeze -l
I'm on Windows 10, python 3.6, with my virtual environment called env activated using command prompt I found that pip freeze -l does not work (error), python -m pip freeze -l does not work (gets global packages) but changing into my virtual environment Scripts directory and running pip freeze or pip freeze -l works. Here is an example of this solution/work-around with my virtual environment, env:
cd \env\Scripts
pip freeze > ..\..\requirements.txt
python venv/Path_to/bin/pip freeze -l
I did try everything, even i inside venv it not worked. This code worked for me. It shows only pip packages in venv.
venv\Scripts\python.exe venv\Scripts\pip.exe freeze > requirements.txt
Install whatever you need to freeze in your virtual environment, and then
pip freeze > requirements.txt
After that install the packages in the virtual environment that you do not want to freeze.
Try the following command:
pip -E /path/to/env/ freeze
Related
I have installed my needed packages (dependencies) in the global system environment instead of my virtual environment (virtualenv) because i have used the command pip install <package-name> outside of the virtual environment.
So i want to know how can i make a list out of them and install them in any of my virtualenvs?
This is useful in a case that anyhow you have installed some packages (dependencies) in your global system environment instead of your virtualenv, by mistake.
For example by using the command "pip install", not "pipenv install" (outside of virtual environment).
So the solution is:
In the global system environment (outside of any virtualenv), create a "requirements.txt" file from all of your installed packages:
$ pip freeze > requirements.txt
Import installed dependencies from the created "requirements.txt" file to the Pipfile by running the below command in the root that the above created file "requirements.txt" exist; but first check:
a) If Pipfile not exist:
$ pipenv install
b) If Pipfile do exist (i.e. already created virtualenv):
$ pipenv install -r requirements.txt
Then your package listing files "Pipfile" & "Pipfile.lock" would be updated and locked.
But I personally recommend that for avoiding this problem to happen, always use the command
$ pipenv install
instead of $ pip install.
You can create file in your global system environment the format of this file is like following:
my_backages.txt
$ pip install -U Flask-SQLAlchemy
$ pip install --upgrade
$ pip install flask
then you can use pipfile as next :
$ pip install -r my_backages.txt
I just created a pipenv environment using pipenv --python 3.9. I then did pipenv shell and started installing packages with pip install. It turns out this doesn't seem to be the usual way of doing things with pipenv. Is there any command I can run to update the Pipfile with all the packages I installed with pip install? I searched but couldn't find anything.
When you have multiple packages you'd like to install, you usually have whats called a requirements.txt file, that contains all the packages you'd like to use for your project.
You can run
$ pipenv run pip freeze > requirements.txt
To generate the requirements file to the current directory you're in, while the virtual environment is active.
Initially you're going to have to install all your packages manually. But after you can run
$ pipenv install -r path/to/requirements.txt
to import all the packages in requirements.txt from the shell/virtual environment.
Instead of running pipenv shell and then pip install <package>, you should simply run pipenv install <package> (outside the virtual environment, from the same location you ran pipenv --python 3.9).
This will install the package in your virtual environment and will automatically update your Pipefile and Pipfile.lock files.
You may skip the Pipfile.lock update by using --skip-lock flag - pipenv install --skip-lock <package>
You can use pipreqs which generates requirements.txt file based on imports.
pip install pipreqs
pipreqs
pipenv install -r requirements.txt
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
I'm trying to go to work with pipenv, but I have problems with the launch of the virtual environment.
errors:
➜ test_pipenv pipenv shell
Launching subshell in virtual environment…
. /home/user/.local/share/virtualenvs/test_pipenv-mzRyHdZF/bin/activate
➜ test_pipenv . /home/user/.local/share/virtualenvs/test_pipenv-mzRyHdZF/bin/activate
cd: **This is not a directory:** /home/user/.local/share/virtualenvs/test_pipenv-mzRyHdZF/bin/activate
or
➜ test_pipenv /home/user/.local/share/virtualenvs/venv-mzRyHdZF/bin/activate
zsh: **Access denied:** /home/user/.local/share/virtualenvs/venv-mzRyHdZF/bin/activate
or
➜ test_pipenv source /home/user/.local/share/virtualenvs/venv-mzRyHdZF/bin/activate
(test_pipenv) ➜ test_pipenv pip freeze
certifi==2018.10.15
chardet==3.0.4
idna==2.7
requests==2.19.1
urllib3==1.23
I installed the pipenv twice and get the same error:
sudo pip install pipenv
and
pip install --user pipenv
I get the same error
I use arch linux and zsh
.zshrc
export PATH=/usr/local/bin:$PATH
export SHELL=/bin/zsh
PIPENV_SHELL=/use/bin/zsh
export ZSH=/home/user/.oh-my-zsh
export PATH="$HOME/.local/bin:$PATH"
Help me please. Why does not it work pipenv shell?
Try doing this:
pip uninstall pipenv
this will uninstall existing pipenv
after doing that re-install pipenv by doing this:
pip install pipenv
check for permissions on your $HOME/.local/share/virtualenvs directory. If you are not allowed to create files/ directories in there, you cannot start a virtual env.
If you are In Arch Linux switch bash to fish.
Then run :
pipenv shell
(can't comment so I'll add this as an answer)
Hi,
From what I can tell you are doing all of this in terminal. If you do not have to work with pipenv, then I would recommend using virtualenv.
Install:
pip install virtualenv
Create a new virtual env:
virtualenv -p python3 your_env_name
load virtual env (from path where created file is located in):
source your_env_name/bin/activate
You can save your packages already installed with pip like this:
pip freeze > requirements.txt
and then load them in your virtual env (once you have loaded it) like this:
pip install -r requirements.txt
Hope this helps! :)
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