Repository: python-poetry/poetry

Issue:
Poetry from `install-poetry.py` does not install packages to system Python even with `POETRY_VIRTUALENVS_CREATE=0`
<!-- Checked checkbox should look like this: [x] -->
- [x] I am on the [latest](https://github.com/python-poetry/poetry/releases/latest) Poetry version.
- [x] I have searched the [issues](https://github.com/python-poetry/poetry/issues) of this repo and believe that this is not a duplicate.

Poetry installed from the [latest `install-poetry.py`](https://github.com/python-poetry/poetry/blob/45a9b8f20384591d0a33ae876bcf23656f928ec0/install-poetry.py) does not install packages to system Python even with `POETRY_VIRTUALENVS_CREATE=0`.

### Steps to reproduce:

Spin up a fresh container for demoing:
```
$ docker run -it python:3.9.2-buster bash
```

In the container:
```
$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/45a9b8f20384591d0a33ae876bcf23656f928ec0/install-poetry.py | python -
$ export PATH="/root/.local/bin:$PATH"
$ export POETRY_VIRTUALENVS_CREATE=0
$ poetry --version
Poetry version 1.1.5
$ poetry init  # go with the bare defaults
```

Install a package (note the "Skipping virtualenv creation" message):
```
$ poetry add django
Skipping virtualenv creation, as specified in config file.
Using version ^3.1.7 for Django

Updating dependencies
Resolving dependencies... (0.6s)

Writing lock file

Package operations: 4 installs, 0 updates, 0 removals

  • Installing asgiref (3.3.1)
  • Installing pytz (2021.1)
  • Installing sqlparse (0.4.1)
  • Installing django (3.1.7)
```

Then try to run
```
$ python -c 'import django'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'django'
```

We can see that `django` got installed to here:
```
$ find / -type d -name django
/root/.local/share/pypoetry/venv/lib/python3.9/site-packages/django
/root/.local/share/pypoetry/venv/lib/python3.9/site-packages/django/forms/templates/django
/root/.local/share/pypoetry/venv/lib/python3.9/site-packages/django/forms/jinja2/django
```

When `pip` would install it here:
```
$ pip install django
$ python -c 'import django'  # no errors
$ find / -type d -name django
/usr/local/lib/python3.9/site-packages/django
/usr/local/lib/python3.9/site-packages/django/forms/templates/django
/usr/local/lib/python3.9/site-packages/django/forms/jinja2/django
/root/.local/share/pypoetry/venv/lib/python3.9/site-packages/django
/root/.local/share/pypoetry/venv/lib/python3.9/site-packages/django/forms/templates/django
/root/.local/share/pypoetry/venv/lib/python3.9/site-packages/django/forms/jinja2/django
```

The same thing also happens with non-root users. This issue does NOT happen with Poetry installed from the [latest `get-poetry.py`](https://github.com/python-poetry/poetry/blob/45a9b8f20384591d0a33ae876bcf23656f928ec0/get-poetry.py) NOR with `poetry==1.1.5` installed from `pip`.




Discussion:
Thanks for testing the new bootstrap method and for reporting this issue!

I think I know where the issue is coming from (Poetry's own virtual environment is mistakenly seen as the system environment).

I'll try to come up with a fix and let you know.
I have the some problem when i use poetry install packages in docker. This is my temporary solution.
```
WORKDIR app
COPY ./pyproject.toml ./poetry.lock*  ./
RUN poetry export -f requirements.txt --output requirements.txt && pip install --no-cache-dir -r requirements.txt
```
I have a possibly related issue. I am working inside a venv that I created by hand not using poetry (with --system-site-package), and poetry while seeing the deps already existing, download and install it again, see below:

```
(.venv) clementwalter@tmp$ poetry config --list
cache-dir = "/home/clementwalter/.cache/pypoetry"
experimental.new-installer = true
installer.parallel = true
virtualenvs.create = false
virtualenvs.in-project = null
virtualenvs.path = "{cache-dir}/virtualenvs"  # /home/clementwalter/.cache/pypoetry/virtualenvs

(.venv) clementwalter@tmp$ poetry env info

Virtualenv
Python:         3.7.10
Implementation: CPython
Path:           /home/clementwalter/tmp/.venv
Valid:          True

System
Platform: linux
OS:       posix
Python:   /opt/miniconda

(.venv) clementwalter@tmp$ poetry add numpy==1.19.2

Updating dependencies
Resolving dependencies... (0.1s)

Writing lock file

Package operations: 0 installs, 1 update, 0 removals

  • Updating numpy (1.19.2 /opt/miniconda/lib/python3.7/site-packages -> 1.19.2)

(.venv) clementwalter@tmp$ python -c "import numpy; print(numpy.__file__)"
/home/clementwalter/tmp/.venv/lib/python3.7/site-packages/numpy/__init__.py

(.venv) clementwalter@tmp$ poetry remove numpy
Updating dependencies
Resolving dependencies... (0.1s)

Writing lock file

Package operations: 0 installs, 0 updates, 1 removal

  • Removing numpy (1.19.2)

(.venv) clementwalter@tmp$ pip install numpy==1.19.2
Requirement already satisfied: numpy==1.19.2 in /opt/miniconda/lib/python3.7/site-packages (1.19.2)

(.venv) clementwalter@tmp$ python -c "import numpy; print(numpy.__file__)"
/opt/miniconda/lib/python3.7/site-packages/numpy/__init__.py
```

So here poetry does an extra install while pip does not and poetry is not supposed to manage env, so why not keeping the version that already exists?
> I have a possibly related issue. I am working inside a venv that I created by hand not using poetry (with --system-site-package), and poetry while seeing the deps already existing, download and install it again, see below:
> 
> ```
> (.venv) clementwalter@tmp$ poetry config --list
> cache-dir = "/home/clementwalter/.cache/pypoetry"
> experimental.new-installer = true
> installer.parallel = true
> virtualenvs.create = false
> virtualenvs.in-project = null
> virtualenvs.path = "{cache-dir}/virtualenvs"  # /home/clementwalter/.cache/pypoetry/virtualenvs
> 
> (.venv) clementwalter@tmp$ poetry env info
> 
> Virtualenv
> Python:         3.7.10
> Implementation: CPython
> Path:           /home/clementwalter/tmp/.venv
> Valid:          True
> 
> System
> Platform: linux
> OS:       posix
> Python:   /opt/miniconda
> 
> (.venv) clementwalter@tmp$ poetry add numpy==1.19.2
> 
> Updating dependencies
> Resolving dependencies... (0.1s)
> 
> Writing lock file
> 
> Package operations: 0 installs, 1 update, 0 removals
> 
>   • Updating numpy (1.19.2 /opt/miniconda/lib/python3.7/site-packages -> 1.19.2)
> 
> (.venv) clementwalter@tmp$ python -c "import numpy; print(numpy.__file__)"
> /home/clementwalter/tmp/.venv/lib/python3.7/site-packages/numpy/__init__.py
> 
> (.venv) clementwalter@tmp$ poetry remove numpy
> Updating dependencies
> Resolving dependencies... (0.1s)
> 
> Writing lock file
> 
> Package operations: 0 installs, 0 updates, 1 removal
> 
>   • Removing numpy (1.19.2)
> 
> (.venv) clementwalter@tmp$ pip install numpy==1.19.2
> Requirement already satisfied: numpy==1.19.2 in /opt/miniconda/lib/python3.7/site-packages (1.19.2)
> 
> (.venv) clementwalter@tmp$ python -c "import numpy; print(numpy.__file__)"
> /opt/miniconda/lib/python3.7/site-packages/numpy/__init__.py
> ```
> 
> So here poetry does an extra install while pip does not and poetry is not supposed to manage env, so why not keeping the version that already exists?

That seems like a separate issue, but I'm not sure.
a :raised_hand_with_fingers_splayed: from me too.  cannot use install-poetry.sh on relatively clean ubuntu 20.04, but get-poetry.sh works perfectly.

I found that I was able to remove this issue on Ubuntu 20.04 by specifying a particular release of `virtualenv` in `install-poetry.py`:

```
517         with temporary_directory() as tmp_dir:
518             subprocess.call(
519                 [sys.executable, "-m", "pip", "install", "virtualenv==20.0.23", "-t", tmp_dir], # <-- changed from just "virtualenv"
520                 stdout=subprocess.PIPE,
521                 stderr=subprocess.STDOUT,
522             )
```



Resolving patch:
diff --git a/poetry/utils/env.py b/poetry/utils/env.py
--- a/poetry/utils/env.py
+++ b/poetry/utils/env.py
@@ -590,7 +590,7 @@ def get(self, reload: bool = False) -> Union["VirtualEnv", "SystemEnv"]:
             create_venv = self._poetry.config.get("virtualenvs.create", True)
 
             if not create_venv:
-                return SystemEnv(Path(sys.prefix))
+                return self.get_system_env()
 
             venv_path = self._poetry.config.get("virtualenvs.path")
             if venv_path is None:
@@ -603,7 +603,7 @@ def get(self, reload: bool = False) -> Union["VirtualEnv", "SystemEnv"]:
             venv = venv_path / name
 
             if not venv.exists():
-                return SystemEnv(Path(sys.prefix))
+                return self.get_system_env()
 
             return VirtualEnv(venv)
 
@@ -872,7 +872,7 @@ def create_venv(
                     "</>"
                 )
 
-                return SystemEnv(Path(sys.prefix))
+                return self.get_system_env()
 
             io.write_line(
                 "Creating virtualenv <c1>{}</> in {}".format(name, str(venv_path))
@@ -1020,7 +1020,7 @@ def get_system_env(cls, naive: bool = False) -> "SystemEnv":
             else:
                 prefix = base_prefix
 
-        return SystemEnv(prefix, base_prefix)
+        return SystemEnv(prefix)
 
     @classmethod
     def get_base_prefix(cls) -> Path:


Test patch: