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
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
About the Pipfile that pipenv generates: when I run pipenv shell in a specific folder, the virtual environment works just fine, and everything I install in there works fine as well, but the Pipfile doesn't seem to be updating with the packages I'm installing. When I check the dependency tree with pipenv graph, it shows all the dependencies I've been using. Is there something I'm missing with how pipenv works?
Note:
Whenever I want to create a new env i follow these steps:
mkdir app
cd app
pipenv shell
pip install <somepackage>
touch main.py # (add my code)
python main.py
You have to install packages using the command pipenv install [package] in order for pipenv to create/update the Pipfile and Pipfile.lock files.
As you already installed the dependencies with pip, you can run pipenv run pip freeze > requirements.txt && pipenv install -r requirements.txt and it will create or update the aforementioned files for you. It is best, though, that you declare each package you want because this method also writes each package dependencies on these files.
Read more here
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
I can use pip install xx --user to install packages for my user. When using pipenv, I can use pipenv install --system to not create a virtual environment but install the requirements on the system, but how can I tell pipenv to use the pip --user flag and install the required packages also only for my current user?
Another question could be, how can I use simple pip to install all requirements from Pipfile?
Setting the environment variables
PIP_USER=1
PIPENV_SYSTEM=1
and running
pipenv install
will install the Pipfile packages to the user's system. As hinted in the question, it is pip that handles installing for a user (the install location), and pipenv that handles installing to the system (the install method).
Note that:
pip’s command line options can be set with environment variables using the format PIP_<UPPER_LONG_NAME> . Dashes (-) have to be replaced with underscores (_).
From the pip User Guide
To install Pipfile requirements using standalone pip, you could generate a requirements.txt using
pipenv lock -r [--dev]
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! :)