# Can't set PYTHONPATH appropriately for pytest in GitHub Actions

I am trying to run pytest for my CI in GitHub Actions, but I can’t, for the life of me, seem to set the PYTHONPATH appropriately. My ci.yml files looks like so:

name: CI

on: [push]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v1 with: python-version:${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Unit tests
run: |
pytest mypackage
env:
PYTHONPATH: "."


Under mypackage I have a directory structure with tests dispersed throughout that directory structure, e.g.

mypackage\foo\bar.py
mypackage\foo\bar_test.py
mypackage\alpha\beta.py
mypackage\alpha\beta_test.py


Locally I add my local directory to the PYTHONPATH so that local imports in the test files work, e.g. I can do:

from mypackage.foo.bar import myfunc

I have all the appropriate __init__.py files in my packages as well. Calling pytest mypackage works fine locally. But in GitHub actions this imports always fails with a ModuleNotFoundError.

Looking through these forums, i thought that setting the PYTHONPATH with the env keyword would solve the problem for me. But it doesn’t. I tried doing PYTHONPATH='.' and setting the absolute path in the GitHub Actions container as well. Neither seem to work. I am lost.

Here’s a screen cap of the error.

pytest works fine locally. This definitely seems like a PYTHONPATH issue, but I am totally lost on how to resolve it.

The default execution working directory for the run script step is /home/runner/work/meml/meml . This is the rootdir of python .
Is mypackage folder located in the root path of your repo? If so, based on this document, please remove the PYTHONPATH env from the unit tests step and use the next snippet:

- name: Unit tests
run: |
pytest mypackage/


Kindly let me know whether this could work for you.

Thanks for the reply. I just tried this – both without and with the PYTHONPATH environment variable defined. It does not work. Still the same problem. The test module cannot import the local modules still!

I also tried some different things to debug, and it’s even more confusing. First, I tried running pytest differently in GitHub actions instead of:

pytest mypackage/


I ran:

python -m pytest mypackage/


I thought this may work better because as it says in the pytest documentation: " This is almost equivalent to invoking the command line script pytest [...] directly, except that calling via python will also add the current directory to sys.path ."

Unfortunately, this did not work either. I get the same error as above.

Then, I tried running classic unittest, rather thay Pytest. One of our files were written with classic unit test, so we could do that. That WORKED. Then, I ran both the normal python invocation of unittest and separately a invocation of the tests using pytest. The unittest works, but pytest does not. Which is really a mystery. See screenshot below.

There is certainly something up with pytest not picking up the PYTHONPATH somehow, but it’s unclear what is happening.

@yanjingzhu I got it to work. But only without the setup-python action. I had to use my own container, then it works. When I try to use the setup-python action, thenpytest can’t find my local modules. I have no idea why that would be the case. Any ideas?

This yml file works

name: Continuous Integration

on: [push]

jobs:
ci:
runs-on: ubuntu-latest
container:
image: docker://manifoldai/orbyter-ml-dev:2.0
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install dependencies
run: |
pip install -r docker/requirements.txt
- name: Black
run: black --check meml
- name: Flake8
run: flake8 meml
- name: Unit Tests
run: |
pytest meml


Beautiful passing tests:

Firstly, I am very glad to hear that you have found a workaround for this issue.
For me to investigate your issue, is it possible to share me your public repo link here? Or could you please create a sample repo which could reproduce this issue and share it here?

I can setup a sample repo later today. Thanks for the help.