Skip to content

Commit

Permalink
👌 CLI: improve create --python versatility
Browse files Browse the repository at this point in the history
Adapt the `--python` option of the `create` command to also allow the user to
simply specify the binary name as long as it is in the `PATH`, for example:

aiida-project create --python python3.9 aiida

will automatically resolve the path to the `python3.9` binary using the
`which` command. To make things even simpler, the `create` command will also
try to resolve the Python binary if only the version is specified, for example:

aiida-project create --python 3.9 aiida
  • Loading branch information
mbercx committed Jun 7, 2023
1 parent f856612 commit d16c4e9
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions aiida_project/commands/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
from datetime import datetime
from pathlib import Path
from typing import List, Optional
Expand Down Expand Up @@ -98,12 +99,9 @@ def create(
List[str], typer.Option("--plugin", "-p", help="Extra plugins to install.")
] = [],
python: Annotated[
Optional[Path],
Optional[str],
typer.Option(
"--python",
exists=True,
dir_okay=False,
file_okay=True,
help="Path to the Python interpreter to use for the environment.",
),
] = None,
Expand Down Expand Up @@ -132,9 +130,21 @@ def create(
venv_path=venv_path,
dir_structure=config.aiida_project_structure,
)
if python is not None:
python_path = Path(python)

if not python_path.exists():
python_which = shutil.which(python)
if python_which is None:
python_which = shutil.which(f"python{python}")
if python_which is None:
print("[bold red]Error:[/bold red] Could not resolve path to Python binary.")
return
else:
python_path = Path(python_which)

typer.echo("✨ Creating the project environment and directory.")
project.create(python_path=python)
project.create(python_path=python_path)

typer.echo("🔧 Adding the AiiDA environment variables to the activate script.")
project.append_activate_text(
Expand Down

0 comments on commit d16c4e9

Please sign in to comment.