{"slug": "when-my-tests-worked-locally-but-ci-couldnt-find-my-code-fixing-pythonpath-in", "title": "When My Tests Worked Locally but CI Couldn’t Find My Code: Fixing PYTHONPATH in CodeBots Arena", "summary": "A developer building CodeBots Arena, an AI vs AI code battle platform, fixed a CI bug where tests passed locally but failed in GitHub Actions due to Python's module search path. The developer updated the CI workflow to set PYTHONPATH to include the server directory, allowing pytest to resolve project modules. The fix is recorded in commit fa401d4.", "body_md": "When My Tests Worked Locally but CI Couldn’t Find My Code: Fixing PYTHONPATH in CodeBots Arena\n\nThis is a submission for DEV’s Summer Bug Smash: Smash Stories powered by Sentry.\n\n🐛 The Bug\n\nWhile developing CodeBots Arena, an AI vs AI code battle platform, I started building automated tests for the battle engine.\n\nThe tests worked in my local development environment.\n\nThen I moved the same test workflow into CI.\n\nThat is where things became interesting.\n\nThe CI environment could not resolve the project modules correctly. The code was there, the tests were there, but Python was not looking in the right place when the tests were executed.\n\nThe problem came down to the Python module search path.\n\n🔍 Local Environment vs. CI\n\nLocally, my development environment already had the project structure and paths configured in a way that allowed imports to work.\n\nCI was a clean environment.\n\nThat difference exposed an assumption in the project:\n\n“The test runner knew where to find the server-side Python package.”\n\nIt did not.\n\nThis is one of those bugs that can be confusing because the code itself looks correct.\n\nThe failure isn’t necessarily caused by the function being tested. It can happen before the test even gets to that point.\n\n🧪 Tracking Down the Problem\n\nI started by looking at the test imports and the project structure.\n\nThe tests needed to import components such as the battle engine, but the CI environment did not automatically include the required project directory in Python’s module search path.\n\nInstead of changing the tests just to make them pass, I wanted the CI environment to correctly understand the project structure.\n\nThat led me to PYTHONPATH.\n\n🛠️ The Fix\n\nI updated the GitHub Actions test step so that the project’s server directory was included in PYTHONPATH before running pytest.\n\nThe CI workflow effectively changed from simply running the tests to explicitly telling Python where the project modules were located.\n\nThe important part was:\n\nenv:\n\nPYTHONPATH: ${{ github.workspace }}/server\n\nThe tests could then be executed with:\n\npython -m pytest tests/ -v\n\nThis change is recorded in the repository history in the commit:\n\nfa401d4 — Add PYTHONPATH to test run step in CI workflow\n\n✅ Verification\n\nAfter the change, the CI test environment could resolve the required Python modules correctly.\n\nThe test workflow was able to move past the import/path problem and execute the automated tests.\n\nThis was a good reminder that “works on my machine” doesn’t always mean “works in CI.”\n\nA clean CI environment can expose assumptions that are invisible during local development.\n\n💡 What I Learned\n\nThe biggest lesson wasn’t just how to set PYTHONPATH.\n\nIt was learning to distinguish between an application bug and an environment/configuration bug.\n\nWhen a test fails, I now try to answer:\n\nThat simple distinction can save a lot of debugging time.\n\n🚧 The Challenge\n\nThe challenging part was that the project itself was available in the CI environment.\n\nThe problem was not that the files were missing.\n\nPython simply didn’t have the correct location in its module search path.\n\nThat made the issue easy to overlook if I focused only on the application code.\n\n📈 Before vs. After\n\nBefore:\n\nThe tests worked in my local development environment, but the CI environment could not correctly resolve the project’s Python modules.\n\nAfter:\n\nThe CI workflow explicitly configured PYTHONPATH to include the server directory, allowing the test runner to locate the required modules and execute the test suite.\n\n🧠 Why This Bug Matters\n\nThis was a relatively small configuration change, but it highlighted an important software engineering principle:\n\nDevelopment environments should not hide assumptions from CI.\n\nAutomated testing is only useful when the environment running those tests is configured correctly.\n\nFor CodeBots Arena, where the battle engine and AI bot components depend on multiple Python modules, making the CI environment predictable is an important part of keeping the project maintainable.\n\n🚀 What I’m Proud Of\n\nI’m proud that the fix was small and targeted.\n\nI didn’t rewrite the test suite or change application logic just to satisfy CI.\n\nI identified the actual environment problem and fixed the configuration at the appropriate layer.\n\nSometimes the best bug fix is the smallest change that addresses the real cause.\n\n🔗 Project\n\nCodeBots Arena is an AI vs AI Code Battle Arena built around automated battles between coding agents.\n\nThe project includes a battle engine, bot SDK, ELO rating system, isolated execution, API components, and automated tests.\n\nGitHub:\n\n[https://github.com/Mhmda1998/CodeBots-Arena](https://github.com/Mhmda1998/CodeBots-Arena)\n\n🎯 Final Takeaway\n\nA test passing locally can give you confidence, but CI gives you a different perspective.\n\nIn my case, CI exposed an assumption that my local environment had been hiding.\n\nThe fix was one configuration change, but the lesson was much bigger:\n\n“When CI says it can’t find your code, don’t immediately rewrite the code. Check the environment first.”", "url": "https://wpnews.pro/news/when-my-tests-worked-locally-but-ci-couldnt-find-my-code-fixing-pythonpath-in", "canonical_source": "https://dev.to/mhmda1998/when-my-tests-worked-locally-but-ci-couldnt-find-my-code-fixing-pythonpath-in-codebots-arena-1l3l", "published_at": "2026-08-19 20:15:15+00:00", "updated_at": "2026-08-19 20:43:55.645757+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["CodeBots Arena", "GitHub Actions", "pytest", "PYTHONPATH", "Sentry"], "alternates": {"html": "https://wpnews.pro/news/when-my-tests-worked-locally-but-ci-couldnt-find-my-code-fixing-pythonpath-in", "markdown": "https://wpnews.pro/news/when-my-tests-worked-locally-but-ci-couldnt-find-my-code-fixing-pythonpath-in.md", "text": "https://wpnews.pro/news/when-my-tests-worked-locally-but-ci-couldnt-find-my-code-fixing-pythonpath-in.txt", "jsonld": "https://wpnews.pro/news/when-my-tests-worked-locally-but-ci-couldnt-find-my-code-fixing-pythonpath-in.jsonld"}}