richardknu
richardknu

Reputation: 3

Package imports not working within PyCharm Python console

I am using Poetry for a project in Python 3.11. Any commands with poetry run ... work fine, but I am having issues when using the Poetry environment as a Python interpreter in PyCharm.

When I use the Python console and try to import psycopg2, I get

ModuleNotFoundError: No module named 'psychopg2'

Running poetry run python -c "import psycopg2" in the terminal works fine. The interpreter configured in PyCharm is the one corresponding to the Poetry environment that I extract via poetry env info -p.

Interestingly, when running using the Poetry environment manually via:

/Users/richardknudsen/Library/Caches/pypoetry/virtualenvs/app-v-qyCfCG-py3.11/bin/python -c "import psycopg2"

I get an error related to processor arch:

ImportError: dlopen(/Users/richardknudsen/Library/Caches/pypoetry/virtualenvs/app-v-qyCfCG-py3.11/lib/python3.11/site-packages/psycopg2/_psycopg.cpython-311-darwin.so, 0x0002): tried: '/Users/richardknudsen/Library/Caches/pypoetry/virtualenvs/app-v-qyCfCG-py3.11/lib/python3.11/site-packages/psycopg2/_psycopg.cpython-311-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/richardknudsen/Library/Caches/pypoetry/virtualenvs/app-v-qyCfCG-py3.11/lib/python3.11/site-packages/psycopg2/_psycopg.cpython-311-darwin.so' (no such file), '/Users/richardknudsen/Library/Caches/pypoetry/virtualenvs/app-v-qyCfCG-py3.11/lib/python3.11/site-packages/psycopg2/_psycopg.cpython-311-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')

Both erros do not occur with all packages, e.g. importing sqlalchemy works fine.

I feel like I am missing some puzzle piece in understanding how poetry works to set it up properly in PyCharm.

Upvotes: 0

Views: 355

Answers (1)

jvllmr
jvllmr

Reputation: 506

I never used PyCharm myself, but when using poetry I usually make poetry create a virtual environment inside my project by configuring poetry globally or using the following poetry.toml file in my project:

[virtualenvs]
create = true
in-project = true

The main benefit: With a local virtual environment it is much easier working with tools that do not support poetry or when the poetry integration has issues finding the right venv. If a tool does not support poetry it at least supports local venvs in a .venv folder.

Edit: It is also way easier to configure your tools because your local python interpreter is always located at .venv/bin/python and not at some randomly generated location.

Upvotes: 0

Related Questions