running a package pytest with poetry

14,641

Solution 1

You need to run poetry install to set up your dev environment. It will install all package and development requirements, and once that is done it will do a dev-install of your source code.

You only need to run it once, code changes will propagate directly and do not require running the install again.


If you have set up the virtual env that you want already, take care that it is activated when you run the install command. If you don't, poetry will try to create a new virtual env and use that, which is probably not what you want.

Solution 2

FYI you also need pytest specified as a dev dependency in pyproject.toml.

If you don't have that, poetry run will find the pytest instance in your home env, but that instance won't find the venv. I don't think the documentation makes that very clear.

Share:
14,641

Related videos on Youtube

Mike
Author by

Mike

Updated on June 04, 2022

Comments

  • Mike
    Mike almost 2 years

    I am new to poetry and want to get it set-up with pytest. I have a package mylib in the following set-up

    ├── dist
    │   ├── mylib-0.0.1-py3-none-any.whl
    │   └── mylib-0.0.1.tar.gz
    ├── poetry.lock
    ├── mylib
    │   ├── functions.py
    │   ├── __init__.py
    │   └── utils.py
    ├── pyproject.toml
    ├── README.md
    └── tests
        └── test_functions.py
    

    in test_functions I have

    import mylib
    

    However, when I run

    poetry run pytest
    

    it complains about mylib not being included. I can run

    pip install dist/mylib-0.0.1-py3-none-any.whl
    

    but that clutters my python environment with mylib. I want to use that environment as well for other packages.

    My question is: What is the proper way to work with poetry and pytest?

    My underlying python environment is a clean pyenv python 3.8. Using pyproject.toml I create a project based virtual environment for mylib.

  • szeitlin
    szeitlin over 2 years
    I get <project> does not contain any element if I try to do poetry install. This is not a helpful error. What is it looking for?