fearless_fool
fearless_fool

Reputation: 35219

How to install pipenv in "externally managed environment"

I want to use pipenv for managing my virtual Python environments. In a freshly built Debian (on RPi4B):

    % pip install pipenv

fails with

error: externally-managed-environment
× This environment is externally managed

and it goes on to suggest that I use a virtual environment. But that's why I'm installing pipenv in the first place!

I could not find updated info on how to install pipenv -- how does one resolve this chicken and egg problem?

Upvotes: 2

Views: 2744

Answers (2)

Human006
Human006

Reputation: 142

I find that Debian maintains a version of pipenv (not necessarily up to date) 1 already available via Debian's internal package manager 'apt'.

I do not believe it is recommended to have nested virtual environments such as when making (pipenv) inside a (venv) case-scenario.

Take for example the code below:

sudo apt install pipenv

(after rebooting and then whilst at the desired directory) Do...

Human006@human006:~/Documents/pprojects $ pipenv shell

Creating a virtualenv for this project...
Pipfile: /home/Human006/Documents/pprojects/Pipfile
Using /usr/bin/python3 (3.11.2) to create virtualenv...
⠸ Creating virtual environment...created virtual environment CPython3.11.2.final.0-64 in 227ms
  creator CPython3Posix(dest=/home/Human006/.local/share/virtualenvs/pprojects-loUQkdxT, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/Human006/.local/share/virtualenv)
    added seed packages: pip==23.0.1, setuptools==66.1.1, wheel==0.38.4
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment!
Virtualenv location: /home/Human006/.local/share/virtualenvs/pprojects-loUQkdxT
Creating a Pipfile for this project...
Launching subshell in virtual environment...
Human006@human006:~/Documents/pprojects $  . /home/Human006/.local/share/virtualenvs/pprojects-loUQkdxT/bin/activate

(pprojects) Human006@human006:~/Documents/pprojects $ 

The process appears to create a directory to separate pipenv packages from system apt packages:

/home/Human006/.local/share/virtualenvs/pprojects-loUQkdxT/bin/activate

A virtual environment was activated within the desired directory aforementioned denoted by the prefix (pprojects):

(pprojects) Human006@human006:~/Documents/pprojects

We then find that pipenv is now tracking packages given by the below:

(pprojects) Human006@human006:~/Documents/pprojects $ dir
Pipfile

Now installing a common package Numpy via pipenv we find:

(pprojects) Human006@human006:~/Documents/pprojects $ dir
Pipfile
(pprojects) Human006@human006:~/Documents/pprojects $ pipenv install numpy
Installing numpy...
Pipfile.lock not found, creating...
Locking [packages] dependencies...
Locking [dev-packages] dependencies...

We find that a Pipfil.lock was created by default. And listing the directory once again we can see this:

(pprojects) Human006@human006:~/Documents/pprojects $ dir
Pipfile  Pipfile.lock

Upvotes: 0

fearless_fool
fearless_fool

Reputation: 35219

Found the answer here: pip install -r requirements.txt is failing: "This environment is externally managed"

Short answer: Use venv to create a venv, then inside that venv, install pipenv.

Upvotes: 0

Related Questions