ImportError on Poetry but module installed

Viewed 3530

I'm using Poetry with VSCode and I'm running into an error when running this

import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions

with beam.Pipeline(options=PipelineOptions()) as p:
    pass  # build your pipeline here

The error is

Traceback (most recent call last):
  File "/path/to/pipeline.py", line 2, in <module>
    import apache_beam as beam
ImportError: No module named apache_beam

My pyproject.toml is

[tool.poetry.dependencies]
python = "^3.7"
pytest = "^5.4.3"
apache-beam = "^2.23.0"

When I run poetry install, I only get this

➜ poetry install
Installing dependencies from lock file

No dependencies to install or update

which makes me think apache-beam was correctly installed.

I'm on VSCode and used these articles (part I, part II) as guide to set-up Poetry.

To start VSCode, I ran poetry shell and then code ., as suggested in the article.

Additional info which may help:

➜ python --version
Python 3.7.3

➜ which python
/Users/miguel/Library/Caches/pypoetry/virtualenvs/python--p5kjR4W-py3.7/bin/python

I have no idea why this is not working and have spent a couple hours on this so any suggestions would be greatly appreciated

EDIT:

I installed python according to this article, which I think might interfere with the virtualenv created by Poetry

2 Answers

Outside of VSCode not knowing which Python environment you want to run with, I would manually check that beam was installed with pip or looking at the packages in the venv itself.

I got the exact same issue with you and I can't figure it out either.

What I know is that when python import dependencies, it will look for dependencies based on sys.path of your python. The dependencies installed by Poetry is on folder

/Users/miguel/Library/Caches/pypoetry/virtualenvs/python--p5kjR4W-py3.7/

apparently the folder is not in sys.path, so the python import failed with ModuleNotFoundError.

The problem become how to add your path where the dependencies resides into the sys.path now? I thought the "poetry shell" cmd will do that, but actually it doesn't. I have no idea then.

Related