-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-virtualenv-for-any-project.html
executable file
·279 lines (254 loc) · 11.1 KB
/
setup-virtualenv-for-any-project.html
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE html>
<html lang="en">
<head>
<title>Setup Virtualenv for Any Project</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Carson's Tech Hut Full Atom Feed" />
<link href="/feeds/python-environment.atom.xml" type="application/atom+xml" rel="alternate" title="Carson's Tech Hut Categories Atom Feed" />
<script>
/* Javascript code to toggle between adding and removing the
"responsive" class to nav when the user clicks on the icon */
function myFunction() {
var x = document.getElementById("nav-bar");
if (x.className === "nav") {
x.className += " responsive";
} else {
x.className = "nav";
}
}
</script>
</head>
<body>
<div class="navigation pure-menu pure-menu-horizontal">
<div class="nav", id="nav-bar">
<a href="/" class="page-container pure-menu-heading pure-menu-link nav">Carson's Tech Hut</a>
<ul class="pure-menu-list">
<li class="pure-menu-item"><a href="../archives.html" class="pure-menu-link">All posts</a></li>
<li class="pure-menu-item"><a href="../categories.html" class="pure-menu-link">Categories</a></li>
<li class="pure-menu-item"><a href="../tags.html" class="pure-menu-link">Tags</a></li>
</ul>
<a href="javascript:void(0);" class="icon pure-menu-link" onclick="myFunction()">
<i class="fas fa-bars"></i>
</a>
</div>
</div>
<div class="page-container">
<div class="entry-content">
<div class="post-meta pure-u">
<div class="pure-u">
<img src="../theme/images/authors/Logo.png" class="post-avatar" alt="Go to the profile of Carson Zhang">
</div>
<div class="pure-u meta-data">
<a href="/author/carson-zhang.html" class="category">Carson Zhang</a><br />
Senior Software Developer
<br />
<abbr title="2018-07-04T00:00:00-06:00">Wed 04 July 2018</abbr>
• 8 min read
</div>
</div>
</div>
<div class="article-header-container">
<div class="background-image-container">
<div class="background-image-small">
<div class="title-container">
<h1>Setup Virtualenv for Any Project</h1>
</div>
</div>
</div>
</div>
<div class="entry-content">
<!--
<div align="right">
Author:<br>
<a href="/author/carson-zhang.html">Carson Zhang</a><br>
</div>
-->
<ul>
<li>Virtualenv is a very handy tool for creating separate virtual environments for python projects.</li>
<li>It is essential and probably mandatory in many cases to use Virtualenv or Virtualenv-like tools (i.e. Anaconda, Pipenv) to run a python project.</li>
<li>In this tutorial, I'll show you how to create a virtualenv for a python project step-by-step. </li>
</ul>
<p><strong>NOTE:</strong> the commends in this tutorial is for Linux/Mac OS, Windows will be slightly different when setting up environment variables and system path</p>
<h2>Setup virtualenv</h2>
<h4>1. Install pip</h4>
<ul>
<li><code>sudo apt-get install python-pip</code></li>
</ul>
<h4>2. Install virtualenv</h4>
<ul>
<li><code>sudo pip install virtualenv</code></li>
</ul>
<h4>3. Create a folder to install virtualenv</h4>
<ul>
<li>Normally this folder would be created inside the project folder<ul>
<li>For example, inside the <strong>Projects</strong> folder</li>
</ul>
</li>
<li>It should be named as <strong>Envs</strong></li>
</ul>
<h4>4. Install virtualenvwrapper</h4>
<ul>
<li><code>sudo pip install virtualenvwrapper</code></li>
<li>This is a very useful tool/package to save you from the hassles of virtualenv</li>
</ul>
<h4>5. Set WORKON_HOME to your virtualenv</h4>
<ul>
<li><code>export WORKON_HOME=~/Desktop/Projects/Env</code></li>
<li>If the virtualenv folder is located at the root directory, use:<ul>
<li><code>export WORKON_HOME=~/.Env</code></li>
</ul>
</li>
</ul>
<h4>6. Add <code>virtualenvwrapper.sh</code> to <code>.bashrc</code> (<strong>NOTE</strong> its <code>.zshrc</code> on Mac)</h4>
<ul>
<li>Navigate to the folder where the virtualenv is installed (normally under /usr/local/bin)</li>
<li><code>nano ~/.bashrc</code> (<code>nano ~/.zshrc</code> on Mac)</li>
<li>Add the following line to the folder:<ul>
<li><code>source /usr/local/bin/virtualenvwrapper.sh</code></li>
</ul>
</li>
<li>If the virtualenvwrapper is installed in another directory, add the path of the directory instead of the <code>/usr/local/bin/</code><ul>
<li>For example, <code>. /Users/Carson/anaconda3/bin/virtualenvwrapper.sh</code></li>
</ul>
</li>
<li><code>CTRL + X</code> to exit, and <code>Y</code> to save, and hit <code>Enter</code></li>
<li>After its being added, use <code>source ~/.bashrc</code> (<code>source ~/.zshrc</code> on Mac) to check if it is working. It should print some files on the screen with no errors<ul>
<li><strong>NOTE</strong> if there is a path issue for virtualenv or python path (i.e. there are multiple versions of Python in system), use <code>which python</code> to find the path, and then add <code>VIRTUALENVWRAPPER_PYTHON=<python path></code> to the <code>bashrc/zshrc</code> </li>
</ul>
</li>
</ul>
<hr>
<h2>Setup virtual environment</h2>
<h4>1. make virtual environment</h4>
<ul>
<li><code>mkvirtualenv env_name --python=python3</code></li>
<li>check python version: <code>python -V</code></li>
</ul>
<h4>2. activate environment</h4>
<ul>
<li>the environment is activated by default when created</li>
<li>to activate an environment: <code>workon env_name</code></li>
</ul>
<h4>3. get the project</h4>
<ul>
<li>navigate to the directory that you would like to download the project</li>
<li>get the project: <code>git clone</code> </li>
<li>From the directory you could tell which python version the project is under</li>
</ul>
<h4>4. get all the nesscary packages</h4>
<ul>
<li>enter the project directory and nevigate to where the requirement file is located</li>
<li><code>pip install -r requirements.txt</code></li>
</ul>
<h4>5. adding apps for the defaul path in virtualenv</h4>
<ul>
<li>sometimes when installing the packages errors may occor, since the apps are put inside the <strong>apps folder</strong> which is not recognized by virtualenv</li>
<li>add the <strong>apps folder</strong>: <code>add2virtualenv apps</code></li>
</ul>
<h4>6. setup default startup folder for the environment</h4>
<ul>
<li>navigate to the folder where you work all the time</li>
<li>setup the folder as default: <code>setvirtualenvproject</code></li>
<li>once the default folder is setup, use <code>cdproject</code> to go back from anywhere</li>
</ul>
<h4>7. install dev tools:</h4>
<ul>
<li>If the project has a different requirements file for the devs, navigate to that folder</li>
<li><code>pip install -r /requirements/dev.txt</code></li>
</ul>
<h4>8. remove environment</h4>
<ul>
<li><code>rmvirtualenv env_name</code></li>
</ul>
<h4>9. tell django which setting files to run in environment:</h4>
<ul>
<li>the goal of this step is for the django to use the customized settings in the <code>/project_name/settings/dev.py</code></li>
<li>go to the folder where the <code>manage.py</code> locates</li>
<li>copy the path generated: <code>pwd</code></li>
<li>go to the virtual environment: <code>cdvirtualenv</code></li>
<li><code>cd bin</code></li>
<li>to enter and edit the file: <code>nano postactivate</code><ul>
<li>put the following lines below the comments:<ul>
<li><code>export DJANGO_SETTINGS_MODULE='project_name.settings.dev'</code></li>
<li><code>export PYTHONPATH=$PYTHONPATH:'path_generated_by_pwd'</code></li>
<li>The first line allows you to use customized settings</li>
<li>The second line allows you to <code>django-admin</code> commands</li>
</ul>
</li>
<li>close and save the file: <code>ctrl + x</code>, type <code>y</code> and press <code>enter</code></li>
</ul>
</li>
<li>to enter and edit the file: <code>nano postdeactivate</code><ul>
<li>put the following lines below the comments:<ul>
<li><code>unset DJANGO_SETTINGS_MODULE</code></li>
</ul>
</li>
</ul>
</li>
<li><strong>deactivate and activate</strong> the environment for the settings to work</li>
</ul>
<h4>10. start django project</h4>
<ul>
<li>if you have done step 9: <code>django-admin runserver</code></li>
<li>if you haven't done step 9: <code>python manage.py runserver</code></li>
</ul>
<h4>11. check the website in browser</h4>
<ul>
<li><code>localhost:8000</code></li>
</ul>
<h4>12. Force refresh the browser</h4>
<ul>
<li>Sometimes when working with multiple projects and switching back and forth, the page display in browser can be scattered since there is cache built-in the browser</li>
<li>to force refresh the browser: <code>ctrl + shift + r</code></li>
</ul>
<h4>13. Some useful virtualenv commands</h4>
<ul>
<li><strong><code>lsvirtualenv</code></strong>: List all of the environments.</li>
<li><strong><code>cdsitepackages</code></strong>: Navigate into the <code>site-package</code> directory of the currently activated virtual environment</li>
<li><strong><code>lssitepackages</code></strong>: Shows contents of <code>site-package</code> directory.</li>
</ul>
</div>
<footer>
<div class="tags">
<a href="/tag/python.html">Python</a>
<a href="/tag/ubuntu.html">Ubuntu</a>
<a href="/tag/linux.html">Linux</a>
<a href="/tag/virtualenv.html">Virtualenv</a>
<a href="/tag/setup.html">setup</a>
</div>
<div class="pure-g post-footer">
<div class="pure-g">
<div class="pure-g poster-info">
<div class="pure-u">
<a href="/author/carson-zhang.html"><img src="../theme/images/authors/Logo.png" alt=""></a>
</div>
<div class="pure-u">
<h3 class="author-name"><a href="/author/carson-zhang.html">Carson Zhang</a></h3>
<p class="author-description">
I'm a senior software developer, passionated with artificial intellgence and
web development.
</p>
</div>
</div>
</div>
<!--
-->
</div>
</footer>
</div>
<footer class="index-footer">
<div class="footer-wide">
<p><a href="">Carson's Tech Hut</a>© Carson Zhang 2020</p>
</div>
<div class="footer-narrow">
<p><a href="">Carson's Tech Hut</a><br>
© Carson Zhang 2020</p>
</div>
</footer>
<link rel="stylesheet" href="../theme/css/pure-min.css" />
<link rel="stylesheet" href="../theme/css/grids-responsive-min.css" />
<link rel="stylesheet" href="../theme/fontawesome-free-5.13.0-web/css/all.min.css" />
<link rel="stylesheet" href="/theme/css/main.css" />
</body>
</html>