Skip to content

Commit 9686cf7

Browse files
committed
use python instead of python3 everywhere
Everywhere: on CI, in tests, in docs. Also, we replace bare pip/pip3 and pytest calls with python -m pip and python -m pytest respectively, to make sure we always use the same python executable everywhere. This fixes #326 (failing Windows tests on CI), because the `python3` executable causes issues (it is not properly copied into the venv directory by venv, which is a CPython bug, see python/cpython#87915), while the python executable does work; see e.g. here https://stackoverflow.com/questions/61669873/python-venv-env-fails-winerror-2-the-system-cannot-find-the-file-specified. `python` should be the same executable under the hood as `python3`, if the setup-python action works as expected, at least. It also allows us to remove the IS_WINDOWS_CI special case. Failed attempts at fixing #326 included: - Setting the shells (assuming something went wrong with environment variables) - Using full paths for both the executable and the venv directory in the template test suite.
1 parent a385157 commit 9686cf7

File tree

7 files changed

+47
-59
lines changed

7 files changed

+47
-59
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ jobs:
2626
- name: Python info
2727
shell: bash -l {0}
2828
run: |
29-
which python3
30-
python3 --version
29+
which python
30+
python --version
3131
- name: Install dependencies
3232
run: |
33-
python3 -m pip install --upgrade pip setuptools
34-
python3 -m pip install .[dev]
33+
python -m pip install --upgrade pip setuptools
34+
python -m pip install .[dev]
3535
- name: Run pytest
3636
run: |
37-
pytest -v
37+
python -m pytest -v

README.dev.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ We recommend installing `cookiecutter` in user space as per `cookiecutter`'s ins
1010
install `cookiecutter` for every new project.
1111

1212
```shell
13-
python3 -m pip install --user --upgrade cookiecutter
13+
python -m pip install --user --upgrade cookiecutter
1414
```
1515

1616
### Get your own copy of the repository
@@ -30,17 +30,17 @@ run the tests later.
3030

3131
```shell
3232
# Create a virtual environment, e.g. with
33-
python3 -m venv env
33+
python -m venv env
3434

3535
# activate virtual environment
3636
source env/bin/activate
3737

3838
# make sure to have a recent version of pip and setuptools
39-
python3 -m pip install --upgrade pip setuptools
39+
python -m pip install --upgrade pip setuptools
4040

4141
# (from the project root directory)
4242
# install development dependencies
43-
python3 -m pip install --no-cache-dir .[dev]
43+
python -m pip install --no-cache-dir .[dev]
4444
```
4545

4646
## Running the tests

tests/test_project.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import os
22
import subprocess
3-
from pathlib import Path
4-
from shutil import which
53
from sys import platform
64
from typing import Sequence
75

86
import pytest
97

108
IS_WINDOWS = platform.startswith('win')
11-
IS_WINDOWS_CI = IS_WINDOWS and os.environ.get('CI', False)
129

1310

1411
def test_project_folder(cookies):
@@ -33,17 +30,11 @@ def run(args: Sequence[str], dirpath: os.PathLike) -> subprocess.CompletedProces
3330

3431
@pytest.fixture
3532
def project_env_bin_dir(tmp_path):
36-
if IS_WINDOWS_CI:
37-
# Creating virtualenv does not work on Windows CI,
38-
# falling back to using current pip3 dir
39-
pip = Path(which('pip3'))
40-
bin_dir = pip.parent
41-
else:
42-
env_output = run(['python3', '-m', 'venv', 'env'], tmp_path)
43-
assert env_output.returncode == 0
44-
bin_dir = str(tmp_path / 'env' / 'bin')
45-
if IS_WINDOWS:
46-
bin_dir = str(tmp_path / 'env' / 'Scripts')
33+
env_output = run(['python', '-m', 'venv', 'env'], tmp_path)
34+
assert env_output.returncode == 0
35+
bin_dir = str(tmp_path / 'env' / 'bin')
36+
if IS_WINDOWS:
37+
bin_dir = str(tmp_path / 'env' / 'Scripts')
4738
return str(bin_dir) + os.sep
4839

4940

@@ -52,17 +43,17 @@ def baked_with_development_dependencies(cookies, project_env_bin_dir):
5243
result = cookies.bake()
5344
assert result.exit_code == 0
5445
bin_dir = project_env_bin_dir
55-
latest_pip_output = run([f'{bin_dir}pip3', 'install', '--upgrade', 'pip', 'setuptools'], result.project)
46+
latest_pip_output = run([f'{bin_dir}python', '-m', 'pip', 'install', '--upgrade', 'pip', 'setuptools'], result.project)
5647
assert latest_pip_output.returncode == 0
57-
pip_output = run([f'{bin_dir}pip3', 'install', '--editable', '.[dev]'], result.project)
48+
pip_output = run([f'{bin_dir}python', '-m', 'pip', 'install', '--editable', '.[dev]'], result.project)
5849
assert pip_output.returncode == 0
5950
return result.project
6051

6152

6253
def test_pytest(baked_with_development_dependencies, project_env_bin_dir):
6354
project_dir = baked_with_development_dependencies
6455
bin_dir = project_env_bin_dir
65-
result = run([f'{bin_dir}pytest'], project_dir)
56+
result = run([f'{bin_dir}python', '-m', 'pytest'], project_dir)
6657
assert result.returncode == 0
6758
assert '== 3 passed in' in result.stdout
6859

@@ -98,9 +89,6 @@ def test_subpackage(baked_with_development_dependencies, project_env_bin_dir):
9889
subsubpackage.mkdir()
9990
(subsubpackage / '__init__.py').write_text('FOO = "bar"', encoding="utf-8")
10091

101-
if IS_WINDOWS_CI:
102-
# On Windows CI python and pip executable are in different paths
103-
bin_dir = ''
10492
# sdist and bdist_wheel both call build command to create build/ dir
10593
# So instead of looking in distribution archives we can look in build/ dir
10694
result = run([f'{bin_dir}python', 'setup.py', 'build'], project_dir)

{{cookiecutter.directory_name}}/.github/workflows/build.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ jobs:
2727
- name: Python info
2828
shell: bash -l {0}
2929
run: |
30-
which python3
31-
python3 --version
30+
which python
31+
python --version
3232
- name: Upgrade pip and install dependencies
3333
run: |
34-
python3 -m pip install --upgrade pip setuptools
35-
python3 -m pip install .[dev,publishing]
34+
python -m pip install --upgrade pip setuptools
35+
python -m pip install .[dev,publishing]
3636
- name: Run unit tests
37-
run: pytest -v
37+
run: python -m pytest -v
3838
- name: Verify that we can build the package
39-
run: python3 setup.py sdist bdist_wheel
39+
run: python setup.py sdist bdist_wheel
4040

4141
lint:
4242
name: Linting build
@@ -52,12 +52,12 @@ jobs:
5252
- name: Python info
5353
shell: bash -l {0}
5454
run: |
55-
which python3
56-
python3 --version
55+
which python
56+
python --version
5757
- name: Upgrade pip and install dependencies
5858
run: |
59-
python3 -m pip install --upgrade pip setuptools
60-
python3 -m pip install .[dev,publishing]
59+
python -m pip install --upgrade pip setuptools
60+
python -m pip install .[dev,publishing]
6161
- name: Check style against standards using prospector
6262
run: prospector
6363
- name: Check import order

{{cookiecutter.directory_name}}/.github/workflows/documentation.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ jobs:
2323
- name: Python info
2424
shell: bash -l {0}
2525
run: |
26-
which python3
27-
python3 --version
26+
which python
27+
python --version
2828
- name: Upgrade pip and install dependencies
2929
run: |
30-
python3 -m pip install --upgrade pip setuptools
31-
python3 -m pip install .[dev,publishing]
30+
python -m pip install --upgrade pip setuptools
31+
python -m pip install .[dev,publishing]
3232
- name: Install pandoc using apt
3333
run: sudo apt install pandoc
3434
- name: Build documentation

{{cookiecutter.directory_name}}/.github/workflows/sonarcloud.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ jobs:
2525
- name: Python info
2626
shell: bash -l {0}
2727
run: |
28-
which python3
29-
python3 --version
28+
which python
29+
python --version
3030
- name: Install dependencies
31-
run: python3 -m pip install .[dev]
31+
run: python -m pip install .[dev]
3232
- name: Check style against standards using prospector
3333
run: prospector --zero-exit --output-format grouped --output-format pylint:pylint-report.txt
3434
- name: Run unit tests with coverage
35-
run: pytest --cov --cov-report term --cov-report xml --junitxml=xunit-result.xml tests/
35+
run: python -m pytest --cov --cov-report term --cov-report xml --junitxml=xunit-result.xml tests/
3636
- name: Correct coverage paths
3737
run: sed -i "s+$PWD/++g" coverage.xml
3838
- name: SonarCloud Scan

{{cookiecutter.directory_name}}/README.dev.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ If you're looking for user documentation, go [here](README.md).
66

77
```shell
88
# Create a virtual environment, e.g. with
9-
python3 -m venv env
9+
python -m venv env
1010

1111
# activate virtual environment
1212
source env/bin/activate
1313

1414
# make sure to have a recent version of pip and setuptools
15-
python3 -m pip install --upgrade pip setuptools
15+
python -m pip install --upgrade pip setuptools
1616

1717
# (from the project root directory)
1818
# install {{ cookiecutter.package_name }} as an editable package
19-
python3 -m pip install --no-cache-dir --editable .
19+
python -m pip install --no-cache-dir --editable .
2020
# install development dependencies
21-
python3 -m pip install --no-cache-dir --editable .[dev]
21+
python -m pip install --no-cache-dir --editable .[dev]
2222
```
2323

2424
Afterwards check that the install directory is present in the `PATH` environment variable.
@@ -156,22 +156,22 @@ cd $(mktemp -d {{ cookiecutter.package_name }}.XXXXXX)
156156
git clone {{ cookiecutter.repository }} .
157157

158158
# prepare a clean virtual environment and activate it
159-
python3 -m venv env
159+
python -m venv env
160160
source env/bin/activate
161161

162162
# make sure to have a recent version of pip and setuptools
163-
python3 -m pip install --upgrade pip setuptools
163+
python -m pip install --upgrade pip setuptools
164164

165165
# install runtime dependencies and publishing dependencies
166-
python3 -m pip install --no-cache-dir .
167-
python3 -m pip install --no-cache-dir .[publishing]
166+
python -m pip install --no-cache-dir .
167+
python -m pip install --no-cache-dir .[publishing]
168168

169169
# clean up any previously generated artefacts
170170
rm -rf {{ cookiecutter.package_name }}.egg-info
171171
rm -rf dist
172172

173173
# create the source distribution and the wheel
174-
python3 setup.py sdist bdist_wheel
174+
python setup.py sdist bdist_wheel
175175

176176
# upload to test pypi instance (requires credentials)
177177
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
@@ -187,14 +187,14 @@ In a new terminal, without an activated virtual environment or an env directory:
187187
cd $(mktemp -d {{ cookiecutter.package_name }}-test.XXXXXX)
188188

189189
# prepare a clean virtual environment and activate it
190-
python3 -m venv env
190+
python -m venv env
191191
source env/bin/activate
192192

193193
# make sure to have a recent version of pip and setuptools
194-
pip install --upgrade pip setuptools
194+
python -m pip install --upgrade pip setuptools
195195

196196
# install from test pypi instance:
197-
python3 -m pip -v install --no-cache-dir \
197+
python -m pip -v install --no-cache-dir \
198198
--index-url https://test.pypi.org/simple/ \
199199
--extra-index-url https://pypi.org/simple {{ cookiecutter.package_name }}
200200
```

0 commit comments

Comments
 (0)