pylyzer
pylyzer copied to clipboard
[Feature] failure to source imported modules from pyenv virtualenv site-packages folder
Problem statement
- Pylyzer was not capable to find imported modules from virtualenv site-packages folder with pyenv.
- on below example I import module openai, but it will work with any python package loaded with pip
- virtualenv is an essential feature for serious pythonistas, without it pylyzer is simply not usable :'(
- Note for @mtshiba : best of luck, besides usability, performance are outstanding and overall project is very promising
Feature Request
I would like to be able to:
- pick by default the virtualenv based on pyenv environment variables
- similar to pyright, specify the
--venv-path /Users/aa/.pyenv/versionswhere are located all my virtualenvs - similar to pyright, specify a
venvsubdirectory name (subdirectory in --venv-path) - ovverride all the above if a pyproject.toml specify a different virtualenv name or path
How to reproduce
The issue below has been reproduced with pylyzer installed with either cargo or mason with lspconfig
On a macbook, I have:
- Installed pyenv with virtualenv via homebrew, setup zsh,
- create and activate a new virtualenv, executed pyenv local, pyenv shell and pyenv global
- use pip to install any python module (e.g openai)
- executed pylyzer on the same virtualenv but it fails to pick imported module (e.g openai)
cd workspace
pyenv virtualenv 3.11.3 my-fresh-new-env
pyenv activate my-fresh-new-env
(my-fresh-new-env) aa@macbook workspace % pip install openai
(my-fresh-new-env) aa@macbook workspace % /Users/aa/.local/share/nvim/mason/bin/pylyzer test.py
Start checking: test.py
Found 3 errors: test.py
Error[#1896]: File test.py, line 2, <module>
2 | import openai
: --------
ImportError: module openai not found
Error[#0624]: File test.py, line 5, <module>
5 | openai.api_key = os.getenv("OPENAI_API_KEY")
: -------
AttributeError: PyModule("openai") object has no attribute api_key
Error[#0624]: File test.py, line 6, <module>.response
6 | response = openai.Model.list()
: -----
AttributeError: PyModule("openai") object has no attribute Model
Content of test.py:
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Model.list()
print(response)
I have tried again after forcing the virtualenv, but same result
pyenv local my-fresh-new-env
pyenv shell my-fresh-new-env
pyenv global my-fresh-new-env
Check our environment variable
Our python, pyenv, virtualenv seems correct as shown below
env | grep -i pyenv
PATH=/Users/aa/.cargo/bin:/usr/local/Cellar/pyenv-virtualenv/1.2.1/shims:/Users/aa/.pyenv/shims:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin
PYENV_SHELL=zsh
PYENV_VIRTUALENV_INIT=1
PYENV_VERSION=my-fresh-new-env
PYENV_ACTIVATE_SHELL=1
PYENV_VIRTUAL_ENV=/Users/aa/.pyenv/versions/3.11.3/envs/my-fresh-new-env
VIRTUAL_ENV=/Users/aa/.pyenv/versions/3.11.3/envs/my-fresh-new-env
the correct python path is picked from our virtualenv:
whereis python
python: /Users/aa/.pyenv/shims/python
Same issue with mason and lspconfig
pylyzer fails short at finding modules in the virtualenv site-packages folder
pylyzer is correclty configured and installed with lspconfig and mason as shown by nvim command :LspInfo
Client: pylyzer (id: 1, bufnr: [1])
filetypes: python
autostart: true
root directory: /Users/aa/workspace
cmd: /Users/aa/.local/share/nvim/mason/bin/pylyzer --server
Configured servers list: gopls, pylyzer
Example of the python imports can be overriden with pyright
cat pyproject.toml
[tool.pyright]
venvPath = "/Users/aa/.pyenv/versions"
venv = "my-fresh-new-env"
This relates to https://github.com/mtshiba/pylyzer/issues/22.
@aakloul you may want to remove your organization id from the screenshot
I'm really looking forward to seeing this feature.
@aakloul you may want to remove your organization id from the screenshot
not mine, it's dummy example :)