-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add pip (with virtual environments) example for Python project #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
The example has been moved to |
|
Moved into examples.md. |
examples.md
Outdated
| ``` | ||
| ``` | ||
| ### Python - pip |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unify format.
| ### Python - pip | |
| ## Python - pip |
|
Please add this to the top. - [Python - pip](#python---pip) |
examples.md
Outdated
| - uses: actions/cache@preview | ||
| with: | ||
| path: .venv | ||
| key: ${{ runner.os }}-pip-${{ hashFiles(format('{0}/{1}', github.workspace, 'requirements.txt')) }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| key: ${{ runner.os }}-pip-${{ hashFiles(format('{0}/{1}', github.workspace, 'requirements.txt')) }} | |
| key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} |
examples.md
Outdated
| - run: | | ||
| python -m venv .venv | ||
| source .venv/bin/activate | ||
| python -m pip install -r requirements.txt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove run section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But .venv path for cache is created by python -m venv .venv command 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just an example of the cache action API, not how to write a Python workflow setup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See an example in other languages/ecosystems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For an example of using virtualenv, changing to the minimum example code and title to Python - pip, virtualenv should enough. (imo)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ummm... .venv is not default package install path.
Default path is/opt/hostedtoolcache/Python/3.7.5/x64/lib/python3.7/site-packages/ for actions/setup-python@v1 with python-version: 3.7
Is /opt/hostedtoolcache/Python/3.7.5/x64/lib/python3.7/site-packages/ better cache path in examples?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the comment above.
|
Here is correct format. key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} |
examples.md
Outdated
| - [Swift, Objective-C - Carthage](#swift-objective-c---carthage) | ||
| - [Swift, Objective-C - CocoaPods](#swift-objective-c---cocoapods) | ||
| - [Ruby - Gem](#ruby---gem) | ||
| - [Python - pip, virtuelenv](#python---pip,-virtuelenv) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| - [Python - pip, virtuelenv](#python---pip,-virtuelenv) | |
| - [Python - pip, virtuelenv](#python---pip-virtuelenv) |
|
If you correct the title link, overall it seems good! |
|
Great! Looks good to me! |
examples.md
Outdated
| - [Swift, Objective-C - Carthage](#swift-objective-c---carthage) | ||
| - [Swift, Objective-C - CocoaPods](#swift-objective-c---cocoapods) | ||
| - [Ruby - Gem](#ruby---gem) | ||
| - [Python - pip, virtuelenv](#python---pip-virtuelenv) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| - [Python - pip, virtuelenv](#python---pip-virtuelenv) | |
| - [Python - pip, virtualenv](#python---pip-virtualenv) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
|
One thing to note about caching the virtualenv is that users will have to manually activate the environment for each task. That might be worth adding. |
|
Cache download cache instead of virtualenv like Travis-CI |
| ```yaml | ||
| - uses: actions/cache@preview | ||
| with: | ||
| path: ~/.cache/pip |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This path only works for Linux, see #3 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh.. thank you for your advice :)
| key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} | ||
| restore-keys: | | ||
| ${{ runner.os }}-pip- | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should also probably be an example for windows here. The path I believe is path: ~\AppData\Local\pip\Cache from pip docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- uses: actions/cache@preview
if: startsWith(runner.os, 'Windows')
with:
path: ~\AppData\Local\pip\Cache
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this!
Could you add an example for windows as well? (See @philipslan's comment)
|
With a step like: - name: Get pip cache
id: pip-cache
run: |
python -c "from pip._internal.locations import USER_CACHE_DIR; print('::set-output name=dir::' + USER_CACHE_DIR)"We can get the cache directory from Once #39 is fixed, we can remove the extra Windows action as we'll be able to use Thus the example would be something like - name: Get pip cache
id: pip-cache
run: |
python -c "from pip._internal.locations import USER_CACHE_DIR; print('::set-output name=dir::' + USER_CACHE_DIR)"
- uses: actions/cache@v1
if: startsWith(runner.os, 'Windows')
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('**\requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- uses: actions/cache@v1
if: !startsWith(runner.os, 'Windows')
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip- |
|
Note the underscore before internal, and the word internal: pip has no public API. And it did change fairly recently: |
|
@hugovk Good point, we shouldn't recommend using an internal API in our docs. @Surgo could you add a Windows example following like: - uses: actions/cache@v1
if: startsWith(runner.os, 'Windows')
with:
path: ~\AppData\Local\pip\Cache
key: ${{ runner.os }}-pip-${{ hashFiles('**\requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-If you're busy or unable to continue this PR, please let me know and I can pick it up. Thanks again for all the work you've done! |
|
Not sure if this is useful for anyone here or at Issue #3, but I've cobbled together a cross-platform cache workflow that uses one preliminary step to find the pip library location, using built-in Python libraries & supported methods. It seems to work on all my projects. It also avoids adding extra skipped build steps. name: CacheTest
on:
push
jobs:
run-tests:
name: ${{ matrix.os }}, ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}-latest
strategy:
max-parallel: 3
matrix:
os: [ubuntu, windows, macOS]
python-version: [3.5, 3.6, 3.7]
steps:
- uses: actions/checkout@v1
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Get Python Location
run: |
echo "::set-output name=path::$(python -c 'import site; print(site.getusersitepackages())')"
id: get_location
- name: Activate Cache
uses: actions/cache@preview
with:
path: ${{ steps.get_location.outputs.path }}
key: pytest-${{ matrix.os }}-${{ matrix.python-version }}-pip-${{ hashFiles('**/**equirement*.txt') }}
restore-keys: |
pytest-${{ matrix.os }}-${{ matrix.python-version }}-pip
- name: Install Dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
pip install --upgrade -r requirements.txt --user
# (Requirements are now installed to the cached user directory.) |
|
Resuming the work for PR at #86 |
|
Sorry my late response 🙇 |
This PR adds example for cache Python packages with virtual environments.
Default install path is like
/opt/hostedtoolcache/Python/3.7.5/x64/lib/python3.7/site-packages/.So use virtual environment to install packages into workspace and cache it.
First time (no cache)
Second time (use cache)