Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.
Sign uppdoc cli ignoring virtual environment #44
Comments
This comment has been minimized.
This comment has been minimized.
|
This is weird because pdoc really just imports. Can you test if calling What is the output of |
This comment has been minimized.
This comment has been minimized.
|
"python -m site?"
This correctly sees the venv layered atop the system-wide 3.6 install.
pdoc3 is installed in both the 3.6 and 3.7 site-packages. |
This comment has been minimized.
This comment has been minimized.
|
Hypothesizing that pdoc executable is linked to the wrong Python (3.7), does it maybe work if you run: python -m pdoc --html batconvertIn that case, maybe also run I'm not familiar with the intricacies of your Windos installation. I'd appreciate your further help in debugging this. |
kernc
added
the
help wanted
label
Mar 26, 2019
This comment has been minimized.
This comment has been minimized.
|
OK, I uninstalled pdoc3 from the 3.7 environment, and now invoking pdoc within the venv is looking at the system 3.6 instance upon which the venv is based. However, it's still not seeing any modules installed within the venv:
I am able to successfully generate results by installing pdoc3 directly within the venv. It seems that without pdoc3 installed within the venv, the shell looks for pdoc.exe first in %VIRTUAL_ENV%\Scripts, doesn't find it there, then works down the %PATH% until it finds it in c:\program files\python36\Scripts. (It was invoking with my user-level 3.7 install previously, as it is ahead of site-wide 3.6 in my %PATH%.) Thus, pdoc.exe is invoking cli.py with the system python.exe from within the (venv) shell, even though %VIRTUAL_ENV% is set to C:\Users\jlaufersweiler\dev\project\venv within that shell. It is still managing to find the module the that was passed as an argument thanks to this part of cli.py:
That last line in the excerpt is how it's loading .\batconvert\__init__.py from C:\Users\jlaufersweiler\dev\project\ despite being in the system context rather than the venv context, since it's adding the current directory to sys.path, but it's not looking in .\venv\Lib\site-packages. So that's what's happening and why. Hoping for a workaround without installing pydoc3 in the venv, I uninstalled it from the venv, deactivated, set include-system-site-packages = true in ./venv/pyenv.cfg, reactivated, and tried again. Unfortunately, while pip list inside the venv shell does now show pdoc3, pdoc --html batconvert still fails to find the modules in .\venv\Lib\site-packages. I think I see why... Here's python -m site after the changes:
The venv shell can find the pdoc modules C:\Program Files\Python36\lib\site-packages now, but without pdoc.exe installed within the venv\Scripts, it can't invoke it with the venv python, since C:\Program Files\Python36\Scripts isn't in the venv's sys.path even when 'C:\Program Files\Python36\lib\site-packages' is. PROPOSED SOLUTION: Add a routine in cli.py that checks to see if the VIRTUAL_ENV environment variable is set, and if so do something like... IMPLEMENTATION QUESTION: Should this always be done, or should it be a switchable behavior? If the latter, should it default to on or off? ... |
This comment has been minimized.
This comment has been minimized.
|
IMPLEMENTATION QUESTION: How should one write tests for something like this? Unit test cases for the environment variable check and sys.path modification operations would be fairly straightforward, but is that enough? Or would proper testing need to actually mock up a venv, with some modules in it that aren't in the standard library, make the example package import them, and then spawn a sub-process shell to enter the venv context and try to run the pdoc cli? |
jlaufersweiler
added a commit
to jlaufersweiler/pdoc
that referenced
this issue
Mar 28, 2019
jlaufersweiler
referenced this issue
Mar 28, 2019
Open
FIX: pdoc cli ignoring virtual environment #45
This comment has been minimized.
This comment has been minimized.
|
To sum up: when virtualenv exists, overlaying system site-packages where pdoc is installed, invoking pdoc doesn't "see" the virtualenv packages because pdoc console script contains a shebang pointing to the system python interpreter, so venv python interpreter is never run, and I think this is a Python issue. PEP 486 and its implementation aimed to make Python Would you consider raising this issue up on the CPython issue tracker? |
kernc
added
the
upstream
label
Apr 1, 2019
This comment has been minimized.
This comment has been minimized.
|
This isn't an instance of the py launcher case in that PEP, though they do touch on similar concepts, and the PR I submitted to fix this here uses a similar approach to the proposed solution for the launcher. What this would really need to be solved at the python-wide level is another flag for venv that would parallel --system-site-packages, say "--system-scripts," that would put the system scripts directory behind the virtualenv's scripts directory in the path when activated, just like --system-site-packages does with site-packages. That being said, venv is in the standard library. A change like that to such an important component would need to start on the dev mailing list, then go through the PEP process. While I agree that it might be worth pursuing at that level, I suggest accepting the PR in the meantime, as it could take months or even years for such a change to get into 3.future. The patch isn't invasive, and could easily be extracted in a future update should such a hypothetical change to venv come to pass. |
This comment has been minimized.
This comment has been minimized.
It's what virtualenv already does by default: $ source venv/bin/activate
$ echo $PATH
/home/user/venv/bin:/home/user/.local/bin:/usr/local/bin:/usr/bin:/binIt just doesn't invoke the correct (current venv's) Python interpreter when the bin script shebang points to another. I think it's a very similar issue to what PEP 486 was intended to fix. I'm not strongly inclined to merging simple workarounds without any input from the parties the issue likely most concerns. I encourage you to show the problem on CPython issue tracker (they'll know what needs or needs not be done) and please reference it here. |
jlaufersweiler commentedMar 22, 2019
Expected Behavior
Runing pdoc3 cli inside active venv shell session:
(venv) C:\Users\me\dev\fooproject>pdoc --html foo
Documentation should be generated based on modules active in the virtual environment.
Actual Behavior
ModuleNotFoundError and ImportError raised for any modules/packages imported by foo that are not present in the system Python directory.
Steps to Reproduce
Additional info