Struct VirtualEnv

Source
pub struct VirtualEnv<'a> { /* private fields */ }
Expand description

A Python virtual environment.

This creates or re-uses a virtual environment. All Python invocations in this environment will have access to the environment’s code, including installed libraries and packages.

Use VirtualEnv::new to create a new environment.

The virtual environment gets deactivated on Drop.

§Example

use xshell_venv::{Shell, VirtualEnv};

let sh = Shell::new()?;
let venv = VirtualEnv::new(&sh, "py3")?;

venv.run("print('Hello World!')")?; // "Hello World!"

Implementations§

Source§

impl<'a> VirtualEnv<'a>

Source

pub fn new(shell: &'a Shell, name: &str) -> Result<VirtualEnv<'a>, Error>

Create a Python virtual environment with the given name.

This creates a new environment or reuses an existing one. Preserves the environment across calls and makes it available for all other commands within the same Shell.

This will try to build a path based on the following environment variables:

  • CARGO_TARGET_DIR
  • OUT_DIR 3 levels up1
  • CARGO_MANIFEST_DIR

1 should usually be the crate’s/workspace’s target directory.

If none of these are set it will use the system’s temporary directory, e.g. /tmp.

§Example
let sh = Shell::new()?;
let venv = VirtualEnv::new(&sh, "py3")?;
Source

pub fn with_path( shell: &'a Shell, venv_dir: &Path, ) -> Result<VirtualEnv<'a>, Error>

Create a Python virtual environment in the given path.

This creates a new environment or reuses an existing one.

§Example
let sh = Shell::new()?;

let mut dir = std::env::temp_dir();
dir.push("xshell-py3");
let venv = VirtualEnv::with_path(&sh, &dir)?;

let output = venv.run("print('hello python')")?;
assert_eq!("hello python", output);
Source

pub fn pip_install(&self, package: &str) -> Result<()>

Install a Python package in this virtual environment.

The package can be anything pip accepts, including specifying the version ($name==1.0.0) or repositories (git+https://github.com/$name/$repo@branch#egg=$name).

§Example
let sh = Shell::new()?;
let venv = VirtualEnv::new(&sh, "py3")?;

venv.pip_install("flake8")?;
let output = venv.run_module("flake8", &["--version"])?;
assert!(output.contains("flake"));
Source

pub fn pip_upgrade(&self, package: &str) -> Result<()>

Upgrade a Python package in this virtual environment.

The package can be anything pip accepts, including specifying the version ($name==1.0.0) or repositories (git+https://github.com/$name/$repo@branch#egg=$name).

§Example
let sh = Shell::new()?;
let venv = VirtualEnv::new(&sh, "py3")?;

venv.pip_install("flake8==3.9.2")?;
let output = venv.run_module("flake8", &["--version"])?;
assert!(output.contains("3.9.2"), "Expected `3.9.2` in output. Got: {}", output);

venv.pip_upgrade("flake8")?;
let output = venv.run_module("flake8", &["--version"])?;
assert!(!output.contains("3.9.2"), "Expected `3.9.2` NOT in output. Got: {}", output);
Source

pub fn run(&self, code: &str) -> Result<String>

Run Python code in this virtual environment.

Returns the code’s output.

§Example
let sh = Shell::new()?;
let venv = VirtualEnv::new(&sh, "py3")?;

let output = venv.run("print('hello python')")?;
assert_eq!("hello python", output);
Source

pub fn run_module(&self, module: &str, args: &[&str]) -> Result<String>

Run library module as a script.

This is python -m $module. Additional arguments are passed through as is.

§Example
let sh = Shell::new()?;
let venv = VirtualEnv::new(&sh, "py3")?;

let output = venv.run_module("pip", &["--version"])?;
assert!(output.contains("pip"));

Auto Trait Implementations§

§

impl<'a> Freeze for VirtualEnv<'a>

§

impl<'a> RefUnwindSafe for VirtualEnv<'a>

§

impl<'a> !Send for VirtualEnv<'a>

§

impl<'a> !Sync for VirtualEnv<'a>

§

impl<'a> Unpin for VirtualEnv<'a>

§

impl<'a> UnwindSafe for VirtualEnv<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.