@@ -360,22 +360,40 @@ fn create_venv_at_path(path: &Path) -> Result<(), Error> {
360
360
return Err ( ret) ;
361
361
} ;
362
362
363
- eprintln ! ( "creating virtual environment at '{}' using '{sys_py}'" , path. display( ) ) ;
364
- let out = Command :: new ( sys_py) . args ( [ "-m" , "virtualenv" ] ) . arg ( path) . output ( ) . unwrap ( ) ;
363
+ // First try venv, which should be packaged in the Python3 standard library.
364
+ // If it is not available, try to create the virtual environment using the
365
+ // virtualenv package.
366
+ if try_create_venv ( sys_py, path, "venv" ) . is_ok ( ) {
367
+ return Ok ( ( ) ) ;
368
+ }
369
+ try_create_venv ( sys_py, path, "virtualenv" )
370
+ }
371
+
372
+ fn try_create_venv ( python : & str , path : & Path , module : & str ) -> Result < ( ) , Error > {
373
+ eprintln ! (
374
+ "creating virtual environment at '{}' using '{python}' and '{module}'" ,
375
+ path. display( )
376
+ ) ;
377
+ let out = Command :: new ( python) . args ( [ "-m" , module] ) . arg ( path) . output ( ) . unwrap ( ) ;
365
378
366
379
if out. status . success ( ) {
367
380
return Ok ( ( ) ) ;
368
381
}
369
382
370
383
let stderr = String :: from_utf8_lossy ( & out. stderr ) ;
371
- let err = if stderr. contains ( "No module named virtualenv" ) {
384
+ let err = if stderr. contains ( & format ! ( "No module named {module}" ) ) {
372
385
Error :: Generic ( format ! (
373
- "virtualenv not found: you may need to install it \
374
- (`{sys_py} -m pip install virtualenv`)"
386
+ r#"{module} not found: you may need to install it:
387
+ `{python} -m pip install {module}`
388
+ If you see an error about "externally managed environment" when running the above command,
389
+ either install `{module}` using your system package manager
390
+ (e.g. `sudo apt-get install {python}-{module}`) or create a virtual environment manually, install
391
+ `{module}` in it and then activate it before running tidy.
392
+ "#
375
393
) )
376
394
} else {
377
395
Error :: Generic ( format ! (
378
- "failed to create venv at '{}' using {sys_py }: {stderr}" ,
396
+ "failed to create venv at '{}' using {python} -m {module }: {stderr}" ,
379
397
path. display( )
380
398
) )
381
399
} ;
0 commit comments