Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make better prediction of the virtualenv site-packages version #12752

Merged
merged 15 commits into from
Jan 29, 2021

Conversation

bxsx
Copy link
Contributor

@bxsx bxsx commented Jan 1, 2021

This PR improves virtualenv integration.

Current implementation assumes that the Python used in virtualenv is in the same version as the Python that is being used to run IPython. This is definitely not truth in most cases. In fact, one of the purpose of using virtualenv is to run different Python's interpreter versions. So the assumption is in opposite of the one of principal goals of the virtualenv idea.

This PR predicts a proper path to the virtualenv's libraries by looking-up at $VIRTUAL_ENV environmental variable against pythonX.Y or pyX.Y substrings. The $VIRTUAL_ENV usually contains Python's version in the name, e.g. project-name-py3.7, project-name-py3.8. If predicted path doesn't exist, the previous functionality is proceed.

Example:

  • Python/system - 3.9
  • Python/virtualenv - 3.7

Results:

  • Current implementation - predicted path: $VIRTUAL_ENV/lib/python3.9/site-packages
  • PR - predicted path: $VIRTUAL_ENV/lib/python3.7/site/packages

bxsx added 10 commits January 1, 2021 18:37
Try to make prediction of the Python's site-package version used by
virtualenv. If predicted path doesn't exist, continue with previous
behaviour.

This can be useful while working with different Python versions in
virtual environments other than system-used (previously provided via
`sys.version_info`). Note that this *only* updates $PYTHONPATH, so
3rd-libraries installed in virtualenv will be avialable. However the
Python interpreter contiunues to be other than the bundled in the
virtualenv  (see `init_virtualenv()` docstring).
@bxsx
Copy link
Contributor Author

bxsx commented Jan 1, 2021

This is interesting fix at 62e6c27. This bug also existed in the current master. It was introduced via #12548. I don't understand why this passed before. I also think it's necessary to patch the Windows part:

diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py
index 1770b4f67..ded20f975 100644
--- a/IPython/core/interactiveshell.py
+++ b/IPython/core/interactiveshell.py
@@ -932,7 +932,7 @@ def init_virtualenv(self):
             return

         if sys.platform == "win32":
-            virtual_env = Path(os.environ["VIRTUAL_ENV"], "Lib", "site-packages")
+            virtual_env = str(Path(os.environ["VIRTUAL_ENV"], "Lib", "site-packages"))
         else:
             venv_path_prefix = Path(os.environ["VIRTUAL_ENV"], "lib")
             venv_path_suffix = Path("site-packages")

On the other hand, AppVeyor build passes. Strange. I don't have access to Windows to test it myself.

@bxsx
Copy link
Contributor Author

bxsx commented Jan 1, 2021

OK. The PR is ready to review. I can also add a commit proposed in the #12752 (comment)

Let me know what do you think. Thanks.

@Carreau Carreau added this to the 8.0 milestone Jan 29, 2021
@Carreau
Copy link
Member

Carreau commented Jan 29, 2021

Thanks, and apologies for the delay. This looks good to me and I'll trust you a s virtualenv user.

@Carreau Carreau merged commit 167f683 into ipython:master Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants