|
| 1 | +from seleniumbase import BaseCase |
| 2 | + |
| 3 | + |
| 4 | +class PythonVirtualEnvPresentation(BaseCase): |
| 5 | + |
| 6 | + def test_py_virtual_envs(self): |
| 7 | + self.create_presentation(theme="serif", transition="slide") |
| 8 | + self.add_slide( |
| 9 | + '<h2>Python Virtual Environments:</h2><br />\n' |
| 10 | + '<h2>What, Why, and How</h2><hr /><br />\n' |
| 11 | + '\n' |
| 12 | + '<h3>Presented by <b>Michael Mintz</b></h3>') |
| 13 | + self.add_slide( |
| 14 | + '<p><b>About me:</b></p>\n' |
| 15 | + '<ul>' |
| 16 | + '<li>I created the <b>SeleniumBase</b> framework.</li>' |
| 17 | + "<li>I'm currently the DevOps Lead at <b>iboss</b>.</li>" |
| 18 | + '</ul>\n', |
| 19 | + image="https://seleniumbase.io/other/iboss_booth.png") |
| 20 | + self.add_slide( |
| 21 | + '<p><b>Topics & tools covered by this presentation:</b></p>' |
| 22 | + '<hr /><br />\n' |
| 23 | + '<ul>' |
| 24 | + '<li>Overview of Virtual Environments</li>' |
| 25 | + '<li>Python package management</li>' |
| 26 | + '<li>Python 3 "venv"</li>' |
| 27 | + '<li>virtualenv / virtualenvwrapper</li>' |
| 28 | + '<li>pip / "pip install"</li>' |
| 29 | + '<li>requirements.txt files</li>' |
| 30 | + '<li>setup.py files</li>' |
| 31 | + '</ul>') |
| 32 | + self.add_slide( |
| 33 | + '<p><b>Topics & tools that are NOT covered here:</b></p><hr />\n' |
| 34 | + '<br /><div><ul>' |
| 35 | + '<li>"conda"</li>' |
| 36 | + '<li>"pipenv"</li>' |
| 37 | + '<li>"poetry"</li>' |
| 38 | + '<li>"pipx"</li>' |
| 39 | + '</ul></div><br />' |
| 40 | + '<p>(Other Python package management tools)</p>') |
| 41 | + self.add_slide( |
| 42 | + '<p><b>What is a Python virtual environment?</b></p><hr /><br />\n' |
| 43 | + '<p>A Python virtual environment is a partitioned directory' |
| 44 | + ' where a Python interpreter, libraries/packages, and scripts' |
| 45 | + ' can be installed and isolated from those installed in other' |
| 46 | + ' virtual environments or the global environment.</p>') |
| 47 | + self.add_slide( |
| 48 | + '<p><b>Why should we use Python virtual environments?</b>' |
| 49 | + '</p><hr /><br />\n' |
| 50 | + '<p>We should use Python virtual environments because different' |
| 51 | + ' Python projects can have conflicting Python dependencies that' |
| 52 | + ' cannot coexist in the same env.</p>') |
| 53 | + self.add_slide( |
| 54 | + '<p><b>Why? - continued</b></p><hr /><br />\n' |
| 55 | + '<p>Example: Project A and Project B both depend on' |
| 56 | + ' different versions of the same Python library!</p>' |
| 57 | + '<p>Therefore, installing the second project requirements' |
| 58 | + ' would overwrite the first one, causing it to break.</p>', |
| 59 | + code=( |
| 60 | + '# Project A requirement:\n' |
| 61 | + 'urllib3==1.25.3\n\n' |
| 62 | + '# Project B requirement:\n' |
| 63 | + 'urllib3==1.26.2')) |
| 64 | + self.add_slide( |
| 65 | + '<p><b>Why? - continued</b></p><hr /><br />\n' |
| 66 | + '<p>It is also possible that Project A and Project B' |
| 67 | + ' require different versions of Python installed!</p>', |
| 68 | + code=( |
| 69 | + '# Project A requirement:\n' |
| 70 | + 'Python-3.8\n\n' |
| 71 | + '# Project B requirement:\n' |
| 72 | + 'Python-2.7')) |
| 73 | + self.add_slide( |
| 74 | + '<p><b>How do we create and use Python virtual envs?</b>' |
| 75 | + '</p><hr /><br />\n' |
| 76 | + '<div><b>There are tools/scripts we can use:</b></div><br />' |
| 77 | + '<ul>' |
| 78 | + '<li>The Python 3 "venv" command</li>' |
| 79 | + '<li>virtualenv / virtualenvwrapper</li>' |
| 80 | + '</ul>') |
| 81 | + self.add_slide( |
| 82 | + '<p><b>Python 3 "venv"</b></p><hr /><br />\n' |
| 83 | + '"venv" creates virtual environments in the location where run' |
| 84 | + ' (generally with Python projects).', |
| 85 | + code=( |
| 86 | + '# Mac / Linux\n' |
| 87 | + 'python3 -m venv ENV_NAME\n' |
| 88 | + 'source ENV_NAME/bin/activate\n\n' |
| 89 | + '# Windows\n' |
| 90 | + 'py -m venv ENV_NAME\n' |
| 91 | + 'call ENV_NAME\\Scripts\\activate\n\n' |
| 92 | + '# (Type "deactivate" to leave a virtual environment.)')) |
| 93 | + self.add_slide( |
| 94 | + '<p><b>"mkvirtualenv" (from virtualenvwrapper)</b></p><hr />\n' |
| 95 | + '<br />"mkvirtualenv" creates virtual environments in one place' |
| 96 | + ' (generally in your home directory).', |
| 97 | + code=( |
| 98 | + '# Mac / Linux\n' |
| 99 | + 'python3 -m pip install virtualenvwrapper\n' |
| 100 | + 'export WORKON_HOME=$HOME/.virtualenvs\n' |
| 101 | + 'source `which virtualenvwrapper.sh`\n' |
| 102 | + 'mkvirtualenv ENV_NAME\n\n' |
| 103 | + '# Windows\n' |
| 104 | + 'py -m pip install virtualenvwrapper-win\n' |
| 105 | + 'mkvirtualenv ENV_NAME\n\n' |
| 106 | + '# (Type "deactivate" to leave a virtual environment.)')) |
| 107 | + self.add_slide( |
| 108 | + '<p><b>List of commands from virtualenvwrapper</b></p>' |
| 109 | + '<hr /><br />', |
| 110 | + code=( |
| 111 | + '# Creating a virtual environment:\n' |
| 112 | + 'mkvirtualenv ENV_NAME\n\n' |
| 113 | + '# Leaving your virtual environment:\n' |
| 114 | + 'deactivate\n\n' |
| 115 | + '# Returning to a virtual environment:\n' |
| 116 | + 'workon ENV_NAME\n\n' |
| 117 | + '# Listing all virtual environments:\n' |
| 118 | + 'lsvirtualenv # OR "workon"\n\n' |
| 119 | + '# Deleting a virtual environment:\n' |
| 120 | + 'rmvirtualenv ENV_NAME')) |
| 121 | + self.add_slide( |
| 122 | + '<p><b>Determining if you are in a virtual env</b></p>' |
| 123 | + '<hr /><br />' |
| 124 | + '<p>When activated, the name of your virtual env' |
| 125 | + ' will appear in parentheses on the left side of your' |
| 126 | + ' command prompt.</p>', |
| 127 | + code=( |
| 128 | + '# Example of how it may look on a Windows machine:\n' |
| 129 | + 'C:\\Users\\Michael\\github> mkvirtualenv my_env\n' |
| 130 | + '(my_env) C:\\Users\\Michael\\github>')) |
| 131 | + self.add_slide( |
| 132 | + '<p><b>Installing packages with "pip install"</b></p><hr /><br />' |
| 133 | + '<p>Once you have created a Python virtual environment and are' |
| 134 | + ' inside, it is now safe to install packages from PyPI,' |
| 135 | + ' setup.py files, and/or requirements.txt files.</p>\n', |
| 136 | + code=( |
| 137 | + '# Install a package from PyPI:\n' |
| 138 | + 'pip install seleniumbase\n\n' |
| 139 | + '# Install packages from a folder with setup.py:\n' |
| 140 | + 'pip install . # Normal installation\n' |
| 141 | + 'pip install -e . # Editable install\n\n' |
| 142 | + '# Install packages from a requirements.txt file:\n' |
| 143 | + 'pip install -r requirements.txt\n')) |
| 144 | + self.add_slide( |
| 145 | + '<p><b>Other useful "pip" commands</b></p><hr /><br />', |
| 146 | + code=( |
| 147 | + '# See which Python packages are installed:\n' |
| 148 | + 'pip list\n\n' |
| 149 | + '# See which installed Python packages are outdated:\n' |
| 150 | + 'pip list --outdated\n\n' |
| 151 | + '# Create a requirements file from installed packages:\n' |
| 152 | + 'pip freeze > my_requirements.txt')) |
| 153 | + self.add_slide( |
| 154 | + '<br /><br /><h2><b>Live Demo Time!</b></h2><hr /><br />', |
| 155 | + image="https://seleniumbase.io/other/python_3d_logo.png") |
| 156 | + self.add_slide( |
| 157 | + '<h2><b>The End. Questions?</b></h2><hr /><br />\n' |
| 158 | + '<h3>Where to find me:</h3>' |
| 159 | + '<ul>' |
| 160 | + '<li><a href="https://github.com/mdmintz">' |
| 161 | + 'https://github.com/mdmintz</a></li>' |
| 162 | + '<li><a href="https://github.com/seleniumbase/SeleniumBase">' |
| 163 | + 'https://github.com/seleniumbase/SeleniumBase</a></li>' |
| 164 | + '<li><a href="https://seleniumbase.io/">' |
| 165 | + 'https://seleniumbase.io/</a></li>' |
| 166 | + '</ul>') |
| 167 | + self.begin_presentation( |
| 168 | + filename="py_virtual_envs.html", show_notes=False, interval=0) |
0 commit comments