Python venv Module
Example
Create a virtual environment programmatically:
import venv
import os
env_dir = 'myenv'
venv.create(env_dir, with_pip=True)
print(f'Virtual environment created at: {env_dir}')
print(f'Exists: {os.path.exists(env_dir)}')
Try it Yourself »
Definition and Usage
The venv module creates lightweight isolated Python environments with their own site directories.
Use it to manage project dependencies separately, avoid version conflicts, or test packages in isolation.
Members
Member | Description |
---|---|
create() | Convenience function to create a virtual environment. |
EnvBuilder | Class for creating virtual environments with customizable behavior. |
EnvBuilder.create() | Create a virtual environment in the specified directory. |
EnvBuilder.create_configuration() | Create pyvenv.cfg configuration file. |
EnvBuilder.ensure_directories() | Create environment directory structure. |
EnvBuilder.install_scripts() | Install activation scripts into the environment. |
EnvBuilder.post_setup() | Hook for post-setup customization. |
EnvBuilder.setup_python() | Set up Python executable in the environment. |
EnvBuilder.setup_scripts() | Set up activation scripts directory. |