Skip to content

Commit dafaabe

Browse files
committed
Merge pull request #567 from qwcode/userscheme_pt3
--user fixes pt 3 Issue #493
2 parents 39bbf2d + 2e1959a commit dafaabe

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

pip/commands/install.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pip.req import InstallRequirement, RequirementSet
66
from pip.req import parse_requirements
77
from pip.log import logger
8-
from pip.locations import build_prefix, src_prefix
8+
from pip.locations import build_prefix, src_prefix, virtualenv_no_global
99
from pip.basecommand import Command
1010
from pip.index import PackageFinder
1111
from pip.exceptions import InstallationError, CommandError
@@ -190,6 +190,8 @@ def run(self, options, args):
190190
options.src_dir = os.path.abspath(options.src_dir)
191191
install_options = options.install_options or []
192192
if options.use_user_site:
193+
if virtualenv_no_global():
194+
raise InstallationError("Can not perform a '--user' install. User site-packages are not visible in this virtualenv.")
193195
install_options.append('--user')
194196
if options.target_dir:
195197
options.ignore_installed = True

pip/locations.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Locations where we look for configs, install stuff, etc"""
22

33
import sys
4+
import site
45
import os
56
import tempfile
67
from pip.backwardcompat import get_python_lib
@@ -13,6 +14,16 @@ def running_under_virtualenv():
1314
"""
1415
return hasattr(sys, 'real_prefix')
1516

17+
def virtualenv_no_global():
18+
"""
19+
Return True if in a venv and no system site packages.
20+
"""
21+
#this mirrors the logic in virtualenv.py for locating the no-global-site-packages.txt file
22+
site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))
23+
no_global_file = os.path.join(site_mod_dir,'no-global-site-packages.txt')
24+
if running_under_virtualenv() and os.path.isfile(no_global_file):
25+
return True
26+
1627

1728
if running_under_virtualenv():
1829
## FIXME: is build/ a good name?

tests/test_user_site.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,14 @@ def test_install_curdir_usersite(self):
8383
assert fspkg_folder in result.files_created, str(result.stdout)
8484

8585
assert egg_info_folder in result.files_created, str(result)
86+
87+
88+
def test_install_user_venv_nositepkgs_fails(self):
89+
"""
90+
user install in virtualenv (with no system packages) fails with message
91+
"""
92+
env = reset_env()
93+
run_from = abspath(join(here, 'packages', 'FSPkg'))
94+
result = run_pip('install', '--user', curdir, cwd=run_from, expect_error=True)
95+
assert "Can not perform a '--user' install. User site-packages are not visible in this virtualenv." in result.stdout
96+

0 commit comments

Comments
 (0)