pylyzer icon indicating copy to clipboard operation
pylyzer copied to clipboard

[Feature] failure to source imported modules from pyenv virtualenv site-packages folder

Open aakloul opened this issue 2 years ago • 4 comments

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/versions where are located all my virtualenvs
  • similar to pyright, specify a venv subdirectory 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:

  1. Installed pyenv with virtualenv via homebrew, setup zsh,
  2. create and activate a new virtualenv, executed pyenv local, pyenv shell and pyenv global
  3. use pip to install any python module (e.g openai)
  4. 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

image

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"

aakloul avatar May 14 '23 04:05 aakloul

This relates to https://github.com/mtshiba/pylyzer/issues/22.

mtshiba avatar May 14 '23 14:05 mtshiba

@aakloul you may want to remove your organization id from the screenshot

VolkerH avatar May 16 '23 08:05 VolkerH

I'm really looking forward to seeing this feature.

AdamMomen avatar May 19 '23 11:05 AdamMomen

@aakloul you may want to remove your organization id from the screenshot

not mine, it's dummy example :)

aakloul avatar Jun 05 '23 23:06 aakloul