Python site.addsitedir() Examples

The following are 30 code examples of site.addsitedir(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module site , or try the search function .
Example #1
Source File: test_site.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def setUpModule():
    global OLD_SYS_PATH
    OLD_SYS_PATH = sys.path[:]

    if site.ENABLE_USER_SITE and not os.path.isdir(site.USER_SITE):
        # need to add user site directory for tests
        try:
            os.makedirs(site.USER_SITE)
            # modify sys.path: will be restored by tearDownModule()
            site.addsitedir(site.USER_SITE)
        except OSError as exc:
            if exc.errno in (errno.EACCES, errno.EPERM):
                raise unittest.SkipTest('unable to create user site directory (%r): %s'
                                        % (site.USER_SITE, exc))
            else:
                raise 
Example #2
Source File: resolver.py    From pipenv with MIT License 6 votes vote down vote up
def _patch_path(pipenv_site=None):
    import site
    pipenv_libdir = os.path.dirname(os.path.abspath(__file__))
    pipenv_site_dir = os.path.dirname(pipenv_libdir)
    pipenv_dist = None
    if pipenv_site is not None:
        pipenv_dist, pipenv_path = find_site_path("pipenv", site_dir=pipenv_site)
    else:
        pipenv_dist, pipenv_path = find_site_path("pipenv", site_dir=pipenv_site_dir)
    if pipenv_dist is not None:
        pipenv_dist.activate()
    else:
        site.addsitedir(next(iter(
            sitedir for sitedir in (pipenv_site, pipenv_site_dir)
            if sitedir is not None
        ), None))
    if pipenv_path is not None:
        pipenv_libdir = pipenv_path
    for _dir in ("vendor", "patched", pipenv_libdir):
        sys.path.insert(0, os.path.join(pipenv_libdir, _dir)) 
Example #3
Source File: __init__.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def add(path, index=1):
  """Insert site dir or virtualenv at a given index in sys.path.

  Args:
    path: relative path to a site dir or virtualenv.
    index: sys.path position to insert the site dir.

  Raises:
    ValueError: path doesn't exist.
  """
  venv_path = os.path.join(path, 'lib', PYTHON_VERSION, 'site-packages')
  if os.path.isdir(venv_path):
    site_dir = venv_path
  elif os.path.isdir(path):
    site_dir = path
  else:
    raise ValueError('virtualenv: cannot access %s: '
                     'No such virtualenv or site directory' % path)



  sys_path = sys.path[:]
  del sys.path[index:]
  site.addsitedir(site_dir)
  sys.path.extend(sys_path[index:]) 
Example #4
Source File: launch.py    From pip-run with MIT License 6 votes vote down vote up
def _inject_sitecustomize(target):
    """
    Create a sitecustomize file in the target that will install
    the target as a sitedir.
    """
    hook = (
        textwrap.dedent(
            """
        import site
        site.addsitedir({target!r})
        """
        )
        .lstrip()
        .format(**locals())
    )
    sc_fn = os.path.join(target, 'sitecustomize.py')
    with open(sc_fn, 'w') as strm:
        strm.write(hook) 
Example #5
Source File: test_bootstrap.py    From shiv with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_import_string(self):
        assert import_string("site.addsitedir") == addsitedir
        assert import_string("site:addsitedir") == addsitedir
        assert import_string("code.interact") == interact
        assert import_string("code:interact") == interact

        # test things not already imported
        func = import_string("os.path:join")
        from os.path import join

        assert func == join

        # test something already imported
        import shiv

        assert import_string("shiv") == shiv == sys.modules["shiv"]

        # test bogus imports raise properly
        with pytest.raises(ImportError):
            import_string("this is bogus!") 
Example #6
Source File: vendor.py    From arithmancer with Apache License 2.0 6 votes vote down vote up
def add(path, index=1):
  """Insert site dir or virtualenv at a given index in sys.path.
  Args:
    path: relative path to a site dir or virtualenv.
    index: sys.path position to insert the site dir.
  Raises:
    ValueError: path doesn't exist.
  """
  venv_path = os.path.join(path, 'lib', PYTHON_VERSION, 'site-packages')
  if os.path.isdir(venv_path):
    site_dir = venv_path
  elif os.path.isdir(path):
    site_dir = path
  else:
    raise ValueError('virtualenv: cannot access %s: '
                     'No such virtualenv or site directory' % path)



  sys_path = sys.path[:]
  del sys.path[index:]
  site.addsitedir(site_dir)
  sys.path.extend(sys_path[index:]) 
Example #7
Source File: flask2postman.py    From flask2postman with MIT License 6 votes vote down vote up
def init_virtualenv():
    venv = os.environ.get("VIRTUAL_ENV", None)
    if not venv:
        return

    p = os.path.normcase(sys.executable)
    paths = [p]
    while os.path.islink(p):
        p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
        paths.append(p)
    venv_path = os.path.normcase(venv)
    if any(p.startswith(venv_path) for p in paths):
        return

    print(venv_warning, file=sys.stderr)
    if sys.platform == "win32":
        path = os.path.join(venv, 'Lib', 'site-packages')
    else:
        python = "python{}.{}".format(*sys.version_info[:2])
        path = os.path.join(venv, 'lib', python, 'site-packages')
    sys.path.insert(0, path)
    site.addsitedir(path) 
Example #8
Source File: start.py    From spyder-kernels with MIT License 6 votes vote down vote up
def import_spydercustomize():
    """Import our customizations into the kernel."""
    here = osp.dirname(__file__)
    parent = osp.dirname(here)
    customize_dir = osp.join(parent, 'customize')

    # Remove current directory from sys.path to prevent kernel
    # crashes when people name Python files or modules with
    # the same name as standard library modules.
    # See spyder-ide/spyder#8007
    while '' in sys.path:
        sys.path.remove('')

    # Import our customizations
    site.addsitedir(customize_dir)
    import spydercustomize

    # Remove our customize path from sys.path
    try:
        sys.path.remove(customize_dir)
    except ValueError:
        pass 
Example #9
Source File: test_site.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def setUpModule():
    global OLD_SYS_PATH
    OLD_SYS_PATH = sys.path[:]

    if site.ENABLE_USER_SITE and not os.path.isdir(site.USER_SITE):
        # need to add user site directory for tests
        try:
            os.makedirs(site.USER_SITE)
            # modify sys.path: will be restored by tearDownModule()
            site.addsitedir(site.USER_SITE)
        except PermissionError as exc:
            raise unittest.SkipTest('unable to create user site directory (%r): %s'
                                    % (site.USER_SITE, exc)) 
Example #10
Source File: test_site.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup() 
Example #11
Source File: databricks_step_main.py    From dagster with Apache License 2.0 5 votes vote down vote up
def main(step_run_ref_filepath, pipeline_zip):
    # Extract any zip files to a temporary directory and add that temporary directory
    # to the site path so the contained files can be imported.
    #
    # We can't rely on pip or other packaging tools because the zipped files might not
    # even be Python packages.
    with tempfile.TemporaryDirectory() as tmp:

        print('Extracting {}'.format(pipeline_zip))
        with zipfile.ZipFile(pipeline_zip) as zf:
            zf.extractall(tmp)
        site.addsitedir(tmp)

        print('Loading step run ref')
        # We can use regular local filesystem APIs to access DBFS inside the Databricks runtime.
        with open(step_run_ref_filepath, 'rb') as handle:
            step_run_ref = pickle.load(handle)

        print('Step run ref:')
        print(step_run_ref)

        print('Setting up storage credentials')
        setup_storage(step_run_ref)

        print('Running pipeline')
        events = list(run_step_from_ref(step_run_ref))

    print('Saving events to DBFS')
    events_filepath = os.path.dirname(step_run_ref_filepath) + '/' + PICKLED_EVENTS_FILE_NAME
    with open(events_filepath, 'wb') as handle:
        pickle.dump(serialize_value(events), handle) 
Example #12
Source File: startup-script.py    From deploymentmanager-samples with Apache License 2.0 5 votes vote down vote up
def expand_machine_type():

    # Force re-evaluation of site-packages so that namespace packages (such
    # as google-auth) are importable. This is needed because we install the
    # packages while this script is running and do not have the benefit of
    # restarting the interpreter for it to do it's usual startup sequence to
    # configure import magic.
    import sys
    import site
    for path in [x for x in sys.path if 'site-packages' in x]:
        site.addsitedir(path)

    import googleapiclient.discovery

    # Assume sockets is 1. Currently, no instances with multiple sockets
    # Assume hyper-threading is on and 2 threads per core
    machine = {'sockets': 1, 'cores': 1, 'threads': 1, 'memory': 1}

    try:
        compute = googleapiclient.discovery.build('compute', 'v1',
                                                  cache_discovery=False)
        type_resp = compute.machineTypes().get(project=PROJECT, zone=ZONE,
                machineType=MACHINE_TYPE).execute()
        if type_resp:
            tot_cpus = type_resp['guestCpus']
            if tot_cpus > 1:
                machine['cores']   = tot_cpus / 2
                machine['threads'] = 2

            # Because the actual memory on the host will be different than what
            # is configured (e.g. kernel will take it). From experiments, about
            # 16 MB per GB are used (plus about 400 MB buffer for the first
            # couple of GB's. Using 30 MB to be safe.
            gb = type_resp['memoryMb'] / 1024;
            machine['memory'] = type_resp['memoryMb'] - (400 + (gb * 30))

    except Exception, e:
        print "Failed to get MachineType '%s' from google api (%s)" % (MACHINE_TYPE, str(e)) 
Example #13
Source File: test_site.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup() 
Example #14
Source File: pytest_pythonpath.py    From pytest-pythonpath with MIT License 5 votes vote down vote up
def pytest_addoption(parser):
    # py.test has an issue where the cwd is not in the PYTHONPATH. Fix it here.
    if os.getcwd() not in sys.path:
        sys.path.insert(0, os.getcwd())
    parser.addini("python_paths", type="pathlist", help="space seperated directory paths to add to PYTHONPATH via sys.path.insert(0, path)",
                  default=[])
    parser.addini("site_dirs", type="pathlist", help="space seperated directory paths to add to PYTHONPATH via site.addsitedir(path)",
                  default=[]) 
Example #15
Source File: test_site.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup() 
Example #16
Source File: pytest_pythonpath.py    From pytest-pythonpath with MIT License 5 votes vote down vote up
def pytest_load_initial_conftests(args, early_config, parser):
    for path in reversed(early_config.getini("python_paths")):
        sys.path.insert(0, str(path))
    for path in early_config.getini("site_dirs"):
        site.addsitedir(str(path)) 
Example #17
Source File: test_site.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup() 
Example #18
Source File: admin.py    From termite-visualizations with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def add_path_first(path):
    sys.path = [path] + [p for p in sys.path if (
        not p == path and not p == (path + '/'))]
    if not global_settings.web2py_runtime_gae:
        site.addsitedir(path) 
Example #19
Source File: test_site.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup() 
Example #20
Source File: test_site.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup() 
Example #21
Source File: bootstrap.py    From knative-lambda-runtime with Apache License 2.0 5 votes vote down vote up
def add_default_site_directories():
    # Set '/var/task as site directory so that we are able to load all customer .pth files
    site.addsitedir(os.environ["LAMBDA_TASK_ROOT"])
    if not is_pythonpath_set():
        site.addsitedir(get_opt_site_packages_directory())
        site.addsitedir(get_opt_python_directory()) 
Example #22
Source File: bootstrap.py    From knative-lambda-runtime with Apache License 2.0 5 votes vote down vote up
def add_default_site_directories():
    # Set '/var/task as site directory so that we are able to load all customer .pth files
    site.addsitedir(os.environ["LAMBDA_TASK_ROOT"])
    if not is_pythonpath_set():
        site.addsitedir(get_opt_site_packages_directory())
        site.addsitedir(get_opt_python_directory()) 
Example #23
Source File: test_site.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup() 
Example #24
Source File: settings.py    From dot15926 with GNU Lesser General Public License v3.0 5 votes vote down vote up
def Apply(self):
        newpaths = self.pypath.toPlainText().split(os.pathsep)
        sys.path = appdata.path
        for v in newpaths:
            site.addsitedir(v.encode(sys.getfilesystemencoding()))
        appconfig['sys.path'] = newpaths
        appconfig['cfg://part4'] = str(self.part4_path.GetPath())
        appconfig['cfg://pca_rdl'] = str(self.pcardl_path.GetPath())
        appconfig['cfg://p7tpl'] = str(self.p7tpl_path.GetPath())

        appconfig.SaveSettings()

        return False 
Example #25
Source File: test_site.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup() 
Example #26
Source File: interactiveshell.py    From Computable with MIT License 5 votes vote down vote up
def init_virtualenv(self):
        """Add a virtualenv to sys.path so the user can import modules from it.
        This isn't perfect: it doesn't use the Python interpreter with which the
        virtualenv was built, and it ignores the --no-site-packages option. A
        warning will appear suggesting the user installs IPython in the
        virtualenv, but for many cases, it probably works well enough.
        
        Adapted from code snippets online.
        
        http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
        """
        if 'VIRTUAL_ENV' not in os.environ:
            # Not in a virtualenv
            return
        
        if sys.executable.startswith(os.environ['VIRTUAL_ENV']):
            # Running properly in the virtualenv, don't need to do anything
            return
        
        warn("Attempting to work in a virtualenv. If you encounter problems, please "
             "install IPython inside the virtualenv.")
        if sys.platform == "win32":
            virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') 
        else:
            virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
                       'python%d.%d' % sys.version_info[:2], 'site-packages')
        
        import site
        sys.path.insert(0, virtual_env)
        site.addsitedir(virtual_env)

    #-------------------------------------------------------------------------
    # Things related to injections into the sys module
    #------------------------------------------------------------------------- 
Example #27
Source File: test_site.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_no_home_directory(self):
        # bpo-10496: getuserbase() and getusersitepackages() must not fail if
        # the current user has no home directory (if expanduser() returns the
        # path unchanged).
        site.USER_SITE = None
        site.USER_BASE = None
        sysconfig._CONFIG_VARS = None

        with EnvironmentVarGuard() as environ, \
             support.swap_attr(os.path, 'expanduser', lambda path: path):

            del environ['PYTHONUSERBASE']
            del environ['APPDATA']

            user_base = site.getuserbase()
            self.assertTrue(user_base.startswith('~' + os.sep),
                            user_base)

            user_site = site.getusersitepackages()
            self.assertTrue(user_site.startswith(user_base), user_site)

        def fake_isdir(path):
            fake_isdir.arg = path
            return False
        fake_isdir.arg = None

        def must_not_be_called(*args):
            raise AssertionError

        with support.swap_attr(os.path, 'isdir', fake_isdir), \
             support.swap_attr(site, 'addsitedir', must_not_be_called), \
             support.swap_attr(site, 'ENABLE_USER_SITE', True):

            # addusersitepackages() must not add user_site to sys.path
            # if it is not an existing directory
            known_paths = set()
            site.addusersitepackages(known_paths)

            self.assertEqual(fake_isdir.arg, user_site)
            self.assertFalse(known_paths) 
Example #28
Source File: test_site.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup() 
Example #29
Source File: vendor.py    From openaps with MIT License 5 votes vote down vote up
def get_module (self):
    site.addsitedir(self.fields.get('path'))
    return importlib.import_module(self.name) 
Example #30
Source File: modules.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def fix_module_search_paths():
  """Add directories that we must be able to import from to path."""
  root_directory = os.environ['ROOT_DIR']
  source_directory = os.path.join(root_directory, 'src')

  python_path = os.getenv('PYTHONPATH', '').split(os.pathsep)

  third_party_libraries_directory = os.path.join(source_directory,
                                                 'third_party')
  config_modules_directory = _config_modules_directory(root_directory)

  if (os.path.exists(config_modules_directory) and
      config_modules_directory not in sys.path):
    sys.path.insert(0, config_modules_directory)
    python_path.insert(0, config_modules_directory)

  if third_party_libraries_directory not in sys.path:
    sys.path.insert(0, third_party_libraries_directory)
    python_path.insert(0, third_party_libraries_directory)

  python_source_directory = os.path.join(source_directory, 'python')
  if python_source_directory not in sys.path:
    sys.path.insert(0, python_source_directory)
    python_path.insert(0, python_source_directory)

  if source_directory not in sys.path:
    sys.path.insert(0, source_directory)
    python_path.insert(0, source_directory)

  os.environ['PYTHONPATH'] = os.pathsep.join(python_path)

  # Add site directory to make from imports work in google namespace.
  site.addsitedir(third_party_libraries_directory)

  # TODO(ochang): Remove this once SDK is removed from images.
  _patch_appengine_modules_for_bots()