Commit 56cf6cc7 by Kelton Bassingthwaite Committed by GitHub

doc: expand information on venv usage

Resolves: #2333
parent 3f7545d6
...@@ -64,6 +64,55 @@ $ poetry add pendulum ...@@ -64,6 +64,55 @@ $ poetry add pendulum
It will automatically find a suitable version constraint **and install** the package and subdependencies. It will automatically find a suitable version constraint **and install** the package and subdependencies.
## Using your virtual environment
By default, poetry creates a virtual environment in `{cache-dir}/virtualenvs` (`{cache-dir}\virtualenvs` on Windows).
You can change the [`cache-dir`](/docs/configuration/#cache-dir) value by editing the poetry config.
Additionally, you can use the [`virtualenvs.in-project`](/docs/configuration/#virtualenvs.in-project) configuration variable
to create virtual environment within your project directory.
There are several ways to run commands within this virtual environment.
### Using `poetry run`
To run your script simply use `poetry run python your_script.py`.
Likewise if you have command line tools such as `pytest` or `black` you can run them using `poetry run pytest`.
### Activating the virtual environment
The easiest way to activate the virtual environment is to create a new shell with `poetry shell`.
To deactivate the virtual environment and exit this new shell type `exit`.
To deactivate the virtual environment without leaving the shell use `deactivate`.
!!!note
**Why a new shell?**
Child processes inherit their environment from their parents, but do not share
them. As such, any modifications made by a child process, is not persisted after
the child process exits. A Python application (Poetry), being a child process,
cannot modify the environment of the shell that it has been called from such
that an activated virtual environment remains active after the Poetry command
has completed execution.
Therefore, Poetry has to create a sub-shell with the virtual envrionment activated
in order for the subsequent commands to run from within the virtual environment.
Alternatively, to avoid creating a new shell, you can manually activate the
virtual environment by running `source {path_to_venv}/bin/activate` (`source {path_to_venv}\Scripts\activate.bat` on Windows).
To get the path to your virtual environment run `poetry env info --path`.
You can also combine these into a nice one-liner, `source $(poetry env info --path)/bin/activate`
To deactivate this virtual environment simply use `deactivate`.
| | POSIX Shell | Windows | Exit/Deactivate |
|-------------------|------------------------------------------------|---------------------------------------------|-----------------|
| New Shell | `poetry shell` | `poetry shell` | `exit` |
| Manual Activation | `source {path_to_venv}/bin/activate` | `source {path_to_venv}\Scripts\activate.bat`| `deactivate` |
| One-liner | ```source`poetry env info --path`/bin/activate```| | `deactivate` |
### Version constraints ### Version constraints
In our example, we are requesting the `pendulum` package with the version constraint `^1.4`. In our example, we are requesting the `pendulum` package with the version constraint `^1.4`.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment