0

I have a webscraper that runs locally, and puts the data in a mysql database hosted on filess.io. Wanted to set up a schedule on github actions to run it consistently, but the build fails here:

 try:
    with connect(
        host=DB_HOST,
        user=DB_USER,
        password=DB_PASSWORD,
        database=DB_DATABASE,
        port=DB_PORT
    ) as connection:
        print(connection)

With this error:

0s
Run python main.py
Traceback (most recent call last):
  File "myscript.py", line 66, in <module>
    with connect(
AttributeError: __enter__
Error: Process completed with exit code 1.

I have secrets set up in github, and the values are pulled into the code in this earlier section, with no errors:

try:
    DB_HOST=os.environ["DB_HOST"]
    DB_USER=os.environ["DB_USER"]
    DB_PASSWORD=os.environ["DB_PASSWORD"]
    DB_DATABASE=os.environ["DB_DATABASE"]
    DB_PORT=os.environ["DB_PORT"]

This code works perfectly on my local machine, with secrets saved in .env file. I have double- and triple-checked that my secrets are set in github. Am I missing something?

I tried running locally (worked fine), logging the github secrets to verify they were stored correctly (was obscured, so that didn't work). Looked up the enter error, and it means some attribute has an error, but I can't figure out which.

Main point of confusion: it works locally. This leads me to believe it's an error with my github setup. Any ideas what's going on?

EDIT: adding github actions workflow code below:

name: Manual workflow
on: [workflow_dispatch]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:

      - name: checkout repo content
        uses: actions/checkout@v2 # checkout the repository content to github runner

      - name: setup python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9' # install the python version needed

      - name: install python packages
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: execute py script # run main.py
        env:
          DB_HOST: ${{ secrets.DB_HOST }}
          DB_USER: ${{ secrets.DB_USER }}
          DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
          DB_DATABASE: ${{ secrets.DB_DATABASE }}
          DB_PORT: ${{ secrets.DB_PORT }}
        run: python main.py
jenanator
  • 1
  • 2
  • Please add your relevant GHA workflow to your question. Thanks! – Azeem Feb 07 '23 at 16:57
  • The `connect` is failing while establishing a connection with MySQL but there's no setup/config to start the MySQL (not the instance on that machine or via Docker using `services`). – Azeem Feb 07 '23 at 17:32
  • I'm using these imports in main.py: `'import mysql.connector from mysql.connector import connect, Error` which makes my code similar to the first example here: https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html Is there more setup/config needed to connect to MySQL? The code works when I run it locally. – jenanator Feb 07 '23 at 19:02

1 Answers1

0

After much digging, my requirements.txt had mysql-connector listed, which is deprecated. My local system had mysql-connector-python installed and was using that. Not sure how the requirements.txt file added the wrong one. Adding mysql-connector-python to the requirements.txt fixed this particular bug.

Thanks to @Azeem for your debugging help!

jenanator
  • 1
  • 2
  • You're welcome! To avoid such dependency issues in the future, you should start using a virtual environment for your Python projects e.g. [`venv`](https://docs.python.org/3/library/venv.html). That'll help contain your dependencies. Good luck! – Azeem Feb 08 '23 at 05:32