Best Python code snippet using prospector_python
virtualenv.py
Source:virtualenv.py
...20 venv_dir = path.join(env.VIRTUALENV_PREFIX, venv_dir)21 else:22 user_home = run('USER_HOME=$(eval echo ~${SUDO_USER}) && echo ${USER_HOME}')23 venv_dir = path.join(user_home, 'w', venv_dir)24 if is_virtualenv(venv_dir):25 return26 if env.VIRTUALENV_BIN:27 virtualenv_bin = env.VIRTUALENV_BIN28 command = '%(virtualenv_bin)s "%(venv_dir)s"' % locals()29 else:30 if package.is_virtualenv_installed_in_system():31 virtualenv_bin = 'virtualenv'32 else:33 virtualenv_bin = '~/.local/bin/virtualenv'34 command = '%(virtualenv_bin)s --quiet "%(venv_dir)s"' % locals()35 run(command)36 if not sub_dirs:37 sub_dirs = ['logs', 'etc', 'tmp']38 if 'VIRTUALENV_SUB_DIRS' in env:39 sub_dirs = list(set(sub_dirs + env.VIRTUALENV_SUB_DIRS))40 for sub_dir in sub_dirs:41 fs.ensure_dir(path.join(venv_dir, sub_dir))42@contextmanager43def activate(venv_dir, local=False):44 """45 ç¨æ¥å¯ç¨VirtualEnvçä¸ä¸æ管çå¨46 ::47 with virtualenv('/path/to/virtualenv'):48 run('python -V')49 .. _virtual environment: http://www.virtualenv.org/50 """51 if not venv_dir.startswith('/'):52 if 'VIRTUALENV_PREFIX' in env:53 venv_dir = path.join(env.VIRTUALENV_PREFIX, venv_dir)54 else:55 user_home = run('USER_HOME=$(eval echo ~${SUDO_USER}) && echo ${USER_HOME}')56 venv_dir = path.join(user_home, 'w', venv_dir)57 if not is_virtualenv(venv_dir):58 raise Exception('æ æèæç¯å¢: %s' % venv_dir)59 join = path.join if local else posixpath.join60 with prefix('. "%s"' % join(venv_dir, 'bin', 'activate')):61 env.CURRENT_VIRTUAL_ENV_DIR = venv_dir62 yield63 # del env['CURRENT_VIRTUAL_ENV_DIR']64def is_virtualenv(venv_dir):65 """å¤ææå®çèæç¯å¢æ¯å¦æ£ç¡®"""66 return exists(path.join(venv_dir, 'bin', 'activate'))67def remove(venv_dir):68 """å é¤æå®çèæç¯å¢"""69 answer = prompt(u"ç¡®å®å é¤èæç¯å¢:%s (y/n)?" % venv_dir)70 if answer.lower() in ['y', 'yes']:71 if is_virtualenv(venv_dir):72 process.kill_by_name(path.join(venv_dir, 'bin'))...
test_python.py
Source:test_python.py
1"""2Python interpreter and environment tests.3These need to be executed with the standard library unittest.4Third party test runners such as pytest cannot be used because5that would interfere with the tests.6"""7import platform8import sys9import unittest10import site11ENV = "@env@"12INTERPRETER = "@interpreter@"13PYTHON_VERSION = "@pythonVersion@"14IS_VIRTUALENV = @is_virtualenv@15IS_VENV = @is_venv@16IS_NIXENV = @is_nixenv@17IS_PYPY = platform.python_implementation() == "PyPy"18class TestCasePython(unittest.TestCase):19 @unittest.skipIf(IS_PYPY, "Executable is incorrect and needs to be fixed.")20 def test_interpreter(self):21 self.assertEqual(sys.executable, INTERPRETER)22 @unittest.skipIf(IS_PYPY, "Prefix is incorrect and needs to be fixed.")23 def test_prefix(self):24 self.assertEqual(sys.prefix, ENV)25 self.assertEqual(sys.prefix, sys.exec_prefix)26 def test_site_prefix(self):27 self.assertTrue(sys.prefix in site.PREFIXES)28 @unittest.skipIf(IS_PYPY or sys.version_info.major==2, "Python 2 does not have base_prefix")29 def test_base_prefix(self):30 if IS_VENV or IS_NIXENV or IS_VIRTUALENV:31 self.assertNotEqual(sys.prefix, sys.base_prefix)32 else:33 self.assertEqual(sys.prefix, sys.base_prefix)34 @unittest.skipIf(sys.version_info.major==3, "sys.real_prefix is only set by virtualenv in case of Python 2.")35 def test_real_prefix(self):36 self.assertTrue(hasattr(sys, "real_prefix") == IS_VIRTUALENV)37 def test_python_version(self):38 self.assertTrue(platform.python_version().startswith(PYTHON_VERSION))39if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!