diff --git a/packages/aws-cdk/lib/init-templates/app/python/README.md b/packages/aws-cdk/lib/init-templates/app/python/README.template.md similarity index 69% rename from packages/aws-cdk/lib/init-templates/app/python/README.md rename to packages/aws-cdk/lib/init-templates/app/python/README.template.md index b6059e6d85872..5bca5316fc006 100644 --- a/packages/aws-cdk/lib/init-templates/app/python/README.md +++ b/packages/aws-cdk/lib/init-templates/app/python/README.template.md @@ -5,22 +5,17 @@ This is a blank project for Python development with CDK. The `cdk.json` file tells the CDK Toolkit how to execute your app. -This project is set up like a standard Python project. The initialization process also creates -a virtualenv within this project, stored under the .env directory. To create the virtualenv -it assumes that there is a `python3` executable in your path with access to the `venv` package. -If for any reason the automatic creation of the virtualenv fails, you can create the virtualenv -manually once the init process completes. +This project is set up like a standard Python project. The initialization +process also creates a virtualenv within this project, stored under the .env +directory. To create the virtualenv it assumes that there is a `python3` +(or `python` for Windows) executable in your path with access to the `venv` +package. If for any reason the automatic creation of the virtualenv fails, +you can create the virtualenv manually. To manually create a virtualenv on MacOS and Linux: ``` -$ python3 -m venv .env -``` - -For Windows, use: - -``` -% python -m venv .env +$ %python-executable% -m venv .env ``` After the init process completes and the virtualenv is created, you can use the following @@ -48,8 +43,8 @@ At this point you can now synthesize the CloudFormation template for this code. $ cdk synth ``` -To add additional dependencies, for example other CDK libraries, just add to -your requirements.txt file and rerun the `pip install -r requirements.txt` +To add additional dependencies, for example other CDK libraries, just add +them to your `setup.py` file and rerun the `pip install -r requirements.txt` command. # Useful commands diff --git a/packages/aws-cdk/lib/init-templates/app/python/cdk.json b/packages/aws-cdk/lib/init-templates/app/python/cdk.json deleted file mode 100644 index 787a71dd6e881..0000000000000 --- a/packages/aws-cdk/lib/init-templates/app/python/cdk.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "app": "python3 app.py" -} diff --git a/packages/aws-cdk/lib/init-templates/app/python/cdk.template.json b/packages/aws-cdk/lib/init-templates/app/python/cdk.template.json new file mode 100644 index 0000000000000..d7293493c4415 --- /dev/null +++ b/packages/aws-cdk/lib/init-templates/app/python/cdk.template.json @@ -0,0 +1,3 @@ +{ + "app": "%python-executable% app.py" +} diff --git a/packages/aws-cdk/lib/init-templates/sample-app/python/README.md b/packages/aws-cdk/lib/init-templates/sample-app/python/README.template.md similarity index 96% rename from packages/aws-cdk/lib/init-templates/sample-app/python/README.md rename to packages/aws-cdk/lib/init-templates/sample-app/python/README.template.md index 05d3afbd3ef95..92caad837d81f 100644 --- a/packages/aws-cdk/lib/init-templates/sample-app/python/README.md +++ b/packages/aws-cdk/lib/init-templates/sample-app/python/README.template.md @@ -15,13 +15,7 @@ manually once the init process completes. To manually create a virtualenv on MacOS and Linux: ``` -$ python3 -m venv .env -``` - -For Windows, use: - -``` -% python -m venv .env +$ %python-executable% -m venv .env ``` After the init process completes and the virtualenv is created, you can use the following diff --git a/packages/aws-cdk/lib/init-templates/sample-app/python/cdk.json b/packages/aws-cdk/lib/init-templates/sample-app/python/cdk.json deleted file mode 100644 index 787a71dd6e881..0000000000000 --- a/packages/aws-cdk/lib/init-templates/sample-app/python/cdk.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "app": "python3 app.py" -} diff --git a/packages/aws-cdk/lib/init-templates/sample-app/python/cdk.template.json b/packages/aws-cdk/lib/init-templates/sample-app/python/cdk.template.json new file mode 100644 index 0000000000000..d7293493c4415 --- /dev/null +++ b/packages/aws-cdk/lib/init-templates/sample-app/python/cdk.template.json @@ -0,0 +1,3 @@ +{ + "app": "%python-executable% app.py" +} diff --git a/packages/aws-cdk/lib/init.ts b/packages/aws-cdk/lib/init.ts index e469467ca1c68..a36a047a9000c 100644 --- a/packages/aws-cdk/lib/init.ts +++ b/packages/aws-cdk/lib/init.ts @@ -42,6 +42,16 @@ export async function cliInit(type?: string, language?: string, canUseNetwork?: await initializeProject(template, language, canUseNetwork !== undefined ? canUseNetwork : true); } +/** + * Returns the name of the Python executable for this OS + */ +function pythonExecutable() { + let python = 'python3'; + if (process.platform === 'win32') { + python = 'python'; + } + return python; +} const INFO_DOT_JSON = 'info.json'; export class InitTemplate { @@ -143,7 +153,8 @@ export class InitTemplate { .replace(/%name\.PascalCased%/g, camelCase(project.name, { pascalCase: true })) .replace(/%cdk-version%/g, cdkVersion) .replace(/%cdk-home%/g, CDK_HOME) - .replace(/%name\.PythonModule%/g, project.name.replace(/-/g, '_')); + .replace(/%name\.PythonModule%/g, project.name.replace(/-/g, '_')) + .replace(/%python-executable%/g, pythonExecutable()); } } @@ -233,7 +244,7 @@ async function postInstall(language: string, canUseNetwork: boolean) { case 'java': return await postInstallJava(canUseNetwork); case 'python': - return await postInstallPython(canUseNetwork); + return await postInstallPython(); } } @@ -263,17 +274,14 @@ async function postInstallJava(canUseNetwork: boolean) { await execute('mvn', 'package'); } -async function postInstallPython(canUseNetwork: boolean) { - if (!canUseNetwork) { - print(`Please run ${colors.green('python -m venv .env')}!`); - return; - } - - print(`Executing ${colors.green('python -m venv .env')}`); +async function postInstallPython() { + const python = pythonExecutable(); + print(`Executing ${colors.green('Creating virtualenv...')}`); try { - await execute('python3', '-m venv', '.env'); + await execute(python, '-m venv', '.env'); } catch (e) { - throw new Error(`${colors.green('python3 -m venv .env')} failed: ` + e.message); + print('Unable to create virtualenv automatically'); + print(`Please run ${colors.green(python + ' -m venv .env')}!`); } }