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