Skip to content

Fix broken installation on Windows in virtualenv #4149

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

Merged
merged 1 commit into from
Oct 25, 2017

Conversation

AleksanderGondek
Copy link
Contributor

Fixes #4142
Python site.py from any virtualenv does not contain many functions added in python 2.7 (see: pypa/virtualenv#355), which results in method site.getsitepackages being absent.

This commit replaces getsitepackages call with equivalent of distutils.sysconfig.get_python_lib which is supported in virtualenv.

@gvanrossum
Copy link
Member

@ethanhs Can you review/test this?

mypy/build.py Outdated
@@ -229,7 +229,7 @@ def default_data_dir(bin_dir: Optional[str]) -> str:
"""
if not bin_dir:
if os.name == 'nt':
prefixes = [os.path.join(sys.prefix, 'Lib'), os.path.join(site.getuserbase(), 'lib')]
prefixes = [os.path.join(sys.prefix, 'Lib'), os.path.join(get_python_lib(), 'lib')]
Copy link
Member

@emmatyping emmatyping Oct 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, this will not work on pip's --user installs. It might be better to trysite.getuserbase(), then fall back on sysconfig's get_python_lib, so it is only invoked in virtualenvs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is too bad.
Do you think that the following solution could do the trick?

getattr(site, 'getuserbase', None)
prefixes = [
    os.path.join(sys.prefix, 'Lib'), 
    os.path.join(getattr(site, 'getuserbase', get_python_lib)(), 'lib')
]

Copy link
Member

@emmatyping emmatyping Oct 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think the getattr call, while concise, is a bit opaque and not obvious. I think

prefixes = [os.path.join(sys.prefix, 'Lib')]
try:
    prefixes.append(os.path.join(site.getuserbase(), 'lib')
except AttributeError:
    prefixes.append(os.path.join(get_python_lib(), 'lib')

or perhaps

prefixes = [os.path.join(sys.prefix, 'Lib')]
if hasattr(site, 'getuserbase'):
    prefixes.append(os.path.join(site.getuserbase(), 'lib')
else:
    prefixes.append(os.path.join(get_python_lib(), 'lib')

would be much more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion looks way more readable, thanks!
Will implement it soonTM (in about 12 hours).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if you could, explain why we fall back on get_python_lib please. eg in the except/else branch
# getuserbase not available in virtualenvs

Thanks so much for fixing this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, glad to help and thanks for your quick feedback!

Fixes python#4142. Python `site.py` from any virtualenv does not contain many functions added in python 2.7 (see: pypa/virtualenv#355), which results in method `site.getsitepackages` being absent.

This commit replaces getsitepackages call with equivalent of `distutils.sysconfig.get_python_lib` which is supported in virtualenv.
@AleksanderGondek
Copy link
Contributor Author

Ping

@gvanrossum
Copy link
Member

gvanrossum commented Oct 25, 2017 via email

@AleksanderGondek
Copy link
Contributor Author

AleksanderGondek commented Oct 25, 2017

I was just wondering if we could merge the change - I have adjusted it accordingly to ethanhs advice.
Sorry if this is being too pushy, but it pains me to not have mypy working on Windows in virtualenv :)

@gvanrossum
Copy link
Member

gvanrossum commented Oct 25, 2017 via email

@AleksanderGondek
Copy link
Contributor Author

Apologies - I have not intended to be disrespectful. I fully appreciate free time put into the project. I will now cease to spam and thanks for clarifying things for me .

Copy link
Member

@emmatyping emmatyping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for fixing this.

@ilevkivskyi ilevkivskyi merged commit 4a10e05 into python:master Oct 25, 2017
JukkaL pushed a commit that referenced this pull request Oct 31, 2017
Fixes #4142. Python `site.py` from any virtualenv does not contain
many functions added in python 2.7
(see: pypa/virtualenv#355), which results in
method `site.getsitepackages` being absent.

This commit replaces getsitepackages call with equivalent of
`distutils.sysconfig.get_python_lib` which is supported in virtualenv.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants