# WithVirtualEnvironment

- Module: [Aspire.Hosting.Python](/reference/api/go/aspire.hosting.python.md)
- Version: `17.0.0`
- Kind: `method`
- Defined on: [`PythonAppResource`](/reference/api/go/aspire.hosting.python/pythonappresource.md)
- Source: [GitHub](https://github.com/microsoft/aspire/tree/bcfa151cdca6d0a573f22d5d9c23f9c727cb3b2f)

Configures a custom virtual environment path for the Python application.

## Remarks

This method updates the Python executable path to use the specified virtual environment.
By default (`createIfNotExists` = `true`), if the virtual environment doesn't exist,
it will be automatically created before running the application (when using pip package manager, not uv).
Set `createIfNotExists` to `false` to disable this behavior and require the venv to already exist.
Virtual environments allow Python applications to have isolated dependencies separate from
the system Python installation. This is the recommended approach for Python applications.
When you explicitly specify a virtual environment path using this method, the path is used verbatim.
The automatic multi-location lookup (checking both app and AppHost directories) only applies when
using the default ".venv" path during initial app creation via AddPythonScript, AddPythonModule, or AddPythonExecutable.

## Definition

```go
func (PythonAppResource) WithVirtualEnvironment(
    virtualEnvironmentPath string,
    options ...*WithVirtualEnvironmentOptions,
) PythonAppResource
```

## Parameters

- `virtualEnvironmentPath` (`string`) -- The path to the virtual environment. Can be absolute or relative to the app directory. When relative, it is resolved from the working directory of the Python application. Common values include ".venv", "venv", or "myenv".
- `options` (`...*WithVirtualEnvironmentOptions`) -- optional settings, see [Options](#options)

## Options

Optional parameters are passed in a trailing `options ...*WithVirtualEnvironmentOptions` argument.

- `CreateIfNotExists` (`*bool`) -- Whether to automatically create the virtual environment if it doesn't exist. Defaults to `true`. Set to `false` to disable automatic venv creation (the venv must already exist).

## Returns

[`PythonAppResource`](/reference/api/go/aspire.hosting.python/pythonappresource.md) -- The resource builder.

Returns a handle; failures surface later via `Err()`.

## Applies to

- [`PythonAppResource`](/reference/api/go/aspire.hosting.python/pythonappresource.md)
- [`UvicornAppResource`](/reference/api/go/aspire.hosting.python/uvicornappresource.md)
