Closed baldurfb closed 2 years ago
Have you tried adding an empty __init__.py
in the connect4 folder?
Yes, there is an empty init.py in the connect4 folder. My project looks like this:
(venv) baldurfb@freyja connect4-server % tree
.
├── Dockerfile
├── Justfile
├── README.md
├── __init__.py
├── docker-compose.yaml
├── k8s
│ ├── configmap.template.yaml
│ ├── deployment.template.yaml
│ ├── ingress.template.yaml
│ ├── job.template.yaml
│ ├── secret.template.yaml
│ └── service.template.yaml
├── migrations
│ ├── README
│ ├── alembic.ini
│ ├── env.py
│ ├── script.py.mako
│ └── versions
│ └── c047b889bc99_initial_migration.py
├── requirements.txt
├── requirements_dev.txt
└── src
├── __init__.py
├── connect4
│ ├── __init__.py
│ ├── app.py
│ ├── app_logic.py
│ ├── config.py
│ ├── converter.py
│ ├── database.py
│ ├── exceptions.py
│ ├── game_logic.py
│ ├── models.py
│ ├── tokens.py
│ └── views.py
└── tests
└── unit
├── __pycache__
│ ├── test_app_logic.cpython-310-pytest-7.2.0.pyc
│ ├── test_converter.cpython-310-pytest-7.2.0.pyc
│ └── test_game_logic.cpython-310-pytest-7.2.0.pyc
├── helper.py
├── test_app_logic.py
├── test_converter.py
└── test_game_logic.py
8 directories, 37 files
What about adding __init__.py
inside the tests
and unit
folders? Not sure if they're both needed, but I have those as well in my project.
Yes, just tried that. Same error.
Note that I am running using (venv) baldurfb@freyja connect4-server % pytest .
. It finds the tests but I don't know where it sets the Python path. I tried running using (venv) baldurfb@freyja connect4-server % python -m pytest
which should set the Python path to the current working directory, but for some reason python refers to my system install, not the venv.
How are you running your tests?
On my local machines (macOS and Linux) I ran pytest ./src/tests/unit
inside the HGOP/src/connect4-server
directory.
I'm not using a Python virtual environment. Are we supposed to be using them? EDIT: I just realized that I have been using venv the whole time ¯\_(ツ)_/¯
It is recommended to always use venv, to avoid version conflicts and such. It is however not required.
Fixed by hacking path.
connect4-server/src/__init__.py
import sys
from pathlib import Path
def _amend_python_path():
path = Path(__file__).parent
sys.path.append(str(path.absolute()))
_amend_python_path()
I would like to add that if you are using VS Code you can add the following to the workspace settings.json
and get code completion and everything working correctly in the IDE:
"python.testing.pytestEnabled": true,
"python.testing.cwd": "${workspaceFolder}/src/connect4-server/src",
"python.autoComplete.extraPaths": [
"${workspaceFolder}/src/connect4-server/src",
],
"python.analysis.extraPaths": [
"${workspaceFolder}/src/connect4-server/src",
],
"python.linting.cwd": "${workspaceFolder}/src/connect4-server/src",
and by adding the file ${workspaceFolder}/src/connect4-server/pytest.ini
:
[pytest]
pythonpath = . src
this should all work correctly, given you have the files set up like the following:
.
└── src
├── connect4-server
│ ├── pytest.ini
│ └── src
│ ├── connect4
│ │ ├── ...
│ └── tests
│ ├── __init__.py
│ └── unit
│ ├── __init__.py
│ ├── ...
Expected Behaviour
Pytest imports the program modules properly
Current Behaviour
When running Pytest locally inside the connect4-server directory I get an error
src/tests/unit/test_game_logic.py:1: in
from connect4 import game_logic
E ModuleNotFoundError: No module named 'connect4'
This is likely because the Python path isn't set correctly. What was your plan for how to run these with the correct path?
Context