Search code examples
pythoninstallationmodulepackagepython-poetry

How do I install python packages with poetry?


I am migrating projects to poetry, but I have encountered a problem.

For example, this is a simple project, not many modules needed. I installed poetry, used poetry add to add the few packages that were required, and then ran poetry install, but it seems like its not installing pandas in my venv.

My pyproject.toml looks like this:

[tool.poetry]
name = "***"
version = "0.1.0"
description = ""
authors = ["***"]

[tool.poetry.dependencies]
python = "^3.9"
pandas = "^1.2.2"
numpy = "^1.20.1"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

Then I run:

PS C:\****> poetry install 
Installing dependencies from lock file

No dependencies to install or update

However, when I run main.py (which uses pandas), it says there is no module named pandas:

PS C:\***> python main.py
Traceback (most recent call last):
  File "C:\***\main.py", line 1, in <module>
    from output import QuestionnaireWrangler, PaymentProbabilityIndex
  File "C:\***\output.py", line 1, in <module>
    import pandas as pd
ModuleNotFoundError: No module named 'pandas'

Am I missing some piece of the puzzle here?


Solution

  • You can run the following commands. First:

    poetry install
    

    Then, you can run either

    poetry python main.py
    

    or

    poetry shell
    python main.py
    

    For production, add the following to your pyproject.toml:

    [tool.poetry.scripts]
    my_app = 'mypackage.my_module:my_method'
    

    Afterwards, run:

    poetry build
    pip install my_project.whl # or tar  
    

    This way, you should be able to run my_app in shell or bash.