forked from mattmakai/fullstackpython.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall.atom.xml
More file actions
912 lines (803 loc) · 71.7 KB
/
all.atom.xml
File metadata and controls
912 lines (803 loc) · 71.7 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Matt Makai</title><link href="http://www.fullstackpython.com/" rel="alternate"></link><link href="http://www.fullstackpython.com/feeds/all.atom.xml" rel="self"></link><id>http://www.fullstackpython.com/</id><updated>2016-05-18T00:00:00-04:00</updated><entry><title>Setting up PostgreSQL with Python 3 and psycopg on Ubuntu 16.04</title><link href="http://www.fullstackpython.com/postgresql-python-3-psycopg2-ubuntu-1604.html" rel="alternate"></link><updated>2016-05-18T00:00:00-04:00</updated><author><name>Matt Makai</name></author><id>tag:www.fullstackpython.com,2016-05-18:postgresql-python-3-psycopg2-ubuntu-1604.html</id><summary type="html"><p><a href="/postgresql.html">PostgreSQL</a> is a powerful open source
<a href="/databases.html">relational database</a> frequently used to create, read,
update and delete <a href="/web-frameworks.html">Python web application</a> data.
<a href="http://pythonhosted.org/psycopg2/">Psycopg2</a> is a PostgreSQL database
driver that serves as a Python client for access to the PostgreSQL server.
This post explains how to install PostgreSQL on Ubuntu 16.04 and run a few
basic SQL queries within a Python program.</p>
<p>We won't cover
<a href="/object-relational-mappers-orms.html">object-relational mappers (ORMs)</a>
in this tutorial but these steps can be used as a prerequisite to working
with an ORM such as SQLAlchemy or Peewee.</p>
<h2>Tools We Need</h2>
<p>Our walkthrough should work with either <a href="/python-2-or-3.html">Python 2 or 3</a>
although all the steps were tested specifically with Python 3.5. Besides
the Python interpreter, here are the other components we'll use:</p>
<ul>
<li><a href="http://releases.ubuntu.com/16.04/">Ubuntu 16.04</a> (these
steps should also work fine with other Ubuntu versions)</li>
<li><a href="https://pip.pypa.io/en/stable/">pip</a> and
<a href="https://virtualenv.pypa.io/en/latest/">virtualenv</a> to handle the
<a href="https://pypi.python.org/pypi/psycopg2/2.6.1">psycopg2</a>
<a href="/application-dependencies.html">application dependency</a></li>
<li><a href="http://www.postgresql.org/">PostgreSQL</a></li>
<li><a href="https://redis-py.readthedocs.io/en/latest/">redis-py</a></li>
</ul>
<p>If you aren't sure how how to install pip and virtualenv, review the
first few steps of the
<a href="/blog/python-3-bottle-gunicorn-ubuntu-1604-xenial-xerus.html">how to set up Python 3, Bottle and Green Unicorn on Ubuntu 16.04 LTS</a>
guide.</p>
<h2>Install PostgreSQL</h2>
<p>We'll install PostgreSQL via the <code>apt</code> package manager. There are a few
packages we need since we want to both run PostgreSQL and use the psycopg2
driver with our Python programs. PostgreSQL will also be installed as a
system service so we can start, stop and reload its configuration when
necessary with the <code>service</code> command. Open the terminal and run: </p>
<div class="highlight"><pre>sudo apt-get install postgresql libpq-dev postgresql-client postgresql-client-common
</pre></div>
<p>Enter your <code>sudo</code> password when prompted and enter 'yes' when <code>apt</code> asks
if you want to install the new packages.</p>
<p><img src="/source/static/img/160518-postgresql-ubuntu-1604/apt-get-postgresql.png" width="100%" class="technical-diagram img-rounded"></p>
<p>After a few moments <code>apt</code> will finish downloading, installing and
processing.</p>
<p><img src="/source/static/img/160518-postgresql-ubuntu-1604/apt-get-postgresql-done.png" width="100%" class="technical-diagram img-rounded"></p>
<p>We now have PostgreSQL installed and the PostgreSQL service is running
in the background. However, we need to create a user and a database instance
to really start using it. Use the <code>sudo</code> command to switch to the new
"postgres" account.</p>
<div class="highlight"><pre>sudo -i -u postgres
</pre></div>
<p>Within the "postgres" account, create a user from the command line with the
<code>createuser</code> command. PostgreSQL will prompt you with several questions.
Answer "n" to superuser and "y" to the other questions.</p>
<div class="highlight"><pre>createuser matt -P --interactive
</pre></div>
<p><img src="/source/static/img/160518-postgresql-ubuntu-1604/createuser.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Awesome, now we have a PostgreSQL user that matches our Ubuntu login
account. Exit out of the postgres account by pressing the "Ctrl" key along
with "d" into the shell. We're back in our own user account.</p>
<p>Create a new database we can use for testing. You can name it "testpython"
or whatever you want for your application.</p>
<div class="highlight"><pre>createdb testpython
</pre></div>
<p>Now we can interact with "testpython" via the PostgreSQL command line tool.</p>
<h2>Interacting with PostgreSQL</h2>
<p>The <code>psql</code> command line client is useful for connecting directly to our
PostgreSQL server without any Python code. Try out <code>psql</code> by using this
command at the prompt: </p>
<div class="highlight"><pre>psql
</pre></div>
<p>The PostgreSQL client will connect to the localhost server. The client is
now ready for input:</p>
<p><img src="/source/static/img/160518-postgresql-ubuntu-1604/postgresql-cli.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Try out PostgreSQL's command prompt a try with commands such as <code>\dt</code> and
<code>\dd</code>. We can also run SQL queries such as "SELECT * from testpython",
although that won't give us back any data yet because we have not inserted
any into the database. A full list of PostgreSQL commands can be found in the
<a href="http://www.postgresql.org/docs/9.6/static/app-psql.html">psql documentation</a>.</p>
<h2>Installing psycopg2</h2>
<p>Now that PostgreSQL is installed and we have a non-superuser account, we
can install the <a href="http://initd.org/psycopg/">psycopg2</a> package. Let's
figure out where our <code>python3</code> executable is located, create a virtualenv
with <code>python3</code>, activate the virtualenv and then install the psycopg2 package
with <code>pip</code>. Find your <code>python3</code> executable using the <code>which</code> command.</p>
<div class="highlight"><pre>which python3
</pre></div>
<p>We will see output like what is in this screenshot.</p>
<p><img src="/source/static/img/160518-postgresql-ubuntu-1604/which-python-3.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Create a new virtualenv in either your home directory or wherever you
store your Python virtualenvs. Specify the full path to your <code>python3</code>
installation. </p>
<div class="highlight"><pre># specify the system python3 installation
virtualenv --python=/usr/bin/python3 venvs/postgrestest
</pre></div>
<p>Activate the virtualenv.</p>
<div class="highlight"><pre>source ~/venvs/postgrestest/bin/activate
</pre></div>
<p>Next we can install the psycopg2 Python package from
<a href="https://pypi.python.org/pypi">PyPI</a> using the <code>pip</code> command.</p>
<div class="highlight"><pre>pip install psycopg2
</pre></div>
<p><img src="/source/static/img/160518-postgresql-ubuntu-1604/pip-install-psycopg2.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Sweet, we've got our PostgreSQL driver installed in our virtualenv! We can
now test out the installation by writing a few lines of Python code.</p>
<h2>Using PostgreSQL from Python</h2>
<p>Launch the Python REPL with the <code>python</code> or <code>python3</code> command. You can also
write the following code in a Python file such as "testpostgres.py" then
execute it with <code>python testpostgres.py</code>. Make sure to replace the "user"
and "password" values with your own.</p>
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">psycopg2</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">connect_str</span> <span class="o">=</span> <span class="s">&quot;dbname=&#39;testpython&#39; user=&#39;matt&#39; host=&#39;localhost&#39; &quot;</span> <span class="o">+</span> \
<span class="s">&quot;password=&#39;myOwnPassword&#39;&quot;</span>
<span class="c"># use our connection values to establish a connection</span>
<span class="n">conn</span> <span class="o">=</span> <span class="n">psycopg2</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">connect_str</span><span class="p">)</span>
<span class="c"># create a psycopg2 cursor that can execute queries</span>
<span class="n">cursor</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="c"># create a new table with a single column called &quot;name&quot;</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;&quot;&quot;CREATE TABLE tutorials (name char(40));&quot;&quot;&quot;</span><span class="p">)</span>
<span class="c"># run a SELECT statement - no data in there, but we can try it</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;&quot;&quot;SELECT * from tutorials&quot;&quot;&quot;</span><span class="p">)</span>
<span class="n">rows</span> <span class="o">=</span> <span class="n">cursor</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="n">rows</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">&quot;Uh oh, can&#39;t connect. Invalid dbname, user or password?&quot;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
</pre></div>
<p>When we run the above code we won't get anything fancy, just an empty
list printed out. However, in those few lines of code we've ensured our
connection to our new database works and we can create new tables in it as
well as query them.</p>
<p><img src="/source/static/img/160518-postgresql-ubuntu-1604/output.png" width="100%" class="technical-diagram img-rounded"></p>
<p>That's just enough of a hook to get started writing more complicated SQL
queries using psycopg2 and PostgreSQL. Make sure to check out the
<a href="/postgresql.html">PostgreSQL</a>,
<a href="/databases.html">relational databases</a> and
<a href="/object-relational-mappers-orms.html">object-relational mappers (ORMs)</a>
pages for more tutorials.</p>
<p>Questions? Tweet <a href="https://twitter.com/fullstackpython">@fullstackpython</a>
or post a message on the
<a href="https://www.facebook.com/fullstackpython">Full Stack Python Facebook page</a>.
Something wrong with this post? Fork
<a href="https://github.com/makaimc/fullstackpython.com/blob/gh-pages/source/content/posts/160518-install-postgresql-python-3-ubuntu-1604.markdown">this page's source on GitHub</a>.</p></summary></entry><entry><title>How to Use Redis with Python 3 and redis-py on Ubuntu 16.04</title><link href="http://www.fullstackpython.com/install-redis-use-python-3-ubuntu-1604.html" rel="alternate"></link><updated>2016-05-16T00:00:00-04:00</updated><author><name>Matt Makai</name></author><id>tag:www.fullstackpython.com,2016-05-16:install-redis-use-python-3-ubuntu-1604.html</id><summary type="html"><p><a href="http://redis.io">Redis</a> is an in-memory key-value pair
<a href="/no-sql-datastore.html">NoSQL data store</a> often used
for <a href="/web-frameworks.html">web application</a> sessions,
transient <a href="/data.html">data</a> and as a broker for
<a href="/task-queues.html">task queues</a>. redis-py is a common Python code
library for interacting with Redis. Let's learn how to get Redis up
and running on Ubuntu and then start using it in a simple Python application.</p>
<h2>Tools We Need</h2>
<p>This tutorial is tested with Python 3.5 but either
<a href="/python-2-or-3.html">Python 2 or 3</a> should work for everything written
here. Just make sure one version is installed on your system by going to
the terminal and typing <code>python --version</code>. Other than Python itself,
here is the software we are going to use throughout the rest of this post:</p>
<ul>
<li><a href="http://releases.ubuntu.com/16.04/">Ubuntu 16.04</a> (these
instructions should work fine with earlier Ubuntu versions as well)</li>
<li><a href="https://pip.pypa.io/en/stable/">pip</a> and
<a href="https://virtualenv.pypa.io/en/latest/">virtualenv</a> to handle the
redis-py <a href="/application-dependencies.html">application dependency</a></li>
<li><a href="http://redis.io">Redis</a></li>
<li><a href="https://redis-py.readthedocs.io/en/latest/">redis-py</a></li>
</ul>
<p>If you aren't sure how how to install pip and virtualenv, review the
first few steps of the
<a href="/blog/python-3-flask-green-unicorn-ubuntu-1604-xenial-xerus.html">how to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS</a>
guide.</p>
<h2>Install Redis</h2>
<p>There are a few ways to install Redis, such as
<a href="http://redis.io/topics/quickstart">downloading and compiling from source</a>.
However, on Ubuntu we can install a system package through <code>apt</code>. The
advantage of this method is that the <code>apt</code> process will take care of
installing <code>redis-server</code> as a system service. Open the terminal and run
the following command:</p>
<div class="highlight"><pre>sudo apt-get install redis-server
</pre></div>
<p>Enter your <code>sudo</code> password and when you are prompted whether you want
to install the new package enter 'yes'.</p>
<p><img src="/source/static/img/160516-redis-ubuntu-1604/apt-get-redis.png" width="100%" class="technical-diagram img-rounded"></p>
<p>After a few moments the downloading and processing should be complete
and you will be back at the prompt.</p>
<p><img src="/source/static/img/160516-redis-ubuntu-1604/apt-get-redis-done.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Redis is now installed and the Redis server is running in the background
as a system service. Even though we installed the <code>redis-server</code> package,
the installation also comes with the Redis command line client. The client
is useful for connecting directly to the Redis server without any Python
code. Give <code>redis-cli</code> a try by typing this into the command prompt:</p>
<div class="highlight"><pre>redis-cli
</pre></div>
<p>The Redis client connects to the localhost server and gives a new prompt
to show it's ready for commands:</p>
<p><img src="/source/static/img/160516-redis-ubuntu-1604/redis-cli.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Give the prompt a try by using Redis commands such as <code>keys *</code> or <code>set a 1</code>.
The full list of Redis commands is provided in the
<a href="http://redis.io/commands">project documentation</a>.</p>
<h2>Virtualenv and Install redis-py</h2>
<p>We need to figure out our <code>python3</code> location, then create a virtualenv,
activate the virtualenv and then install redis-py with <code>pip</code>.
Determine your <code>python3</code> executable location with the <code>which</code> command.</p>
<div class="highlight"><pre>which python3
</pre></div>
<p>You'll see some output like the following screenshot.</p>
<p><img src="/source/static/img/160516-redis-ubuntu-1604/which-python-3.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Create a new virtualenv either in your home directory or wherever you
store your project virtualenvs. Specify the full path to your <code>python3</code>
installation. </p>
<div class="highlight"><pre># specify the system python3 installation
virtualenv --python=/usr/bin/python3 venvs/redistest
</pre></div>
<p>Activate the virtualenv.</p>
<div class="highlight"><pre>source ~/venvs/redistest/bin/activate
</pre></div>
<p>Next we can install the redis-py Python package from
<a href="https://pypi.python.org/pypi">PyPI</a> using the <code>pip</code> command.</p>
<div class="highlight"><pre>pip install redis
</pre></div>
<p><img src="/source/static/img/160516-redis-ubuntu-1604/pip-install-redis.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Alright, now it is installed in our virtualenv. Let's write some simple
Python code to try out give redis-py!</p>
<h2>Working with Redis from Python</h2>
<p>Fire up the Python REPL with the <code>python</code> or <code>python3</code> command. You can also
write the following code in a Python file such as "testredis.py" then
execute it with <code>python testredis.py</code>.</p>
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">redis</span>
<span class="c"># create a connection to the localhost Redis server instance, by</span>
<span class="c"># default it runs on port 6379</span>
<span class="n">redis_db</span> <span class="o">=</span> <span class="n">redis</span><span class="o">.</span><span class="n">StrictRedis</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="s">&quot;localhost&quot;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">6379</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="c"># see what keys are in Redis</span>
<span class="n">redis_db</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="c"># output for keys() should be an empty list &quot;[]&quot;</span>
<span class="n">redis_db</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s">&#39;full stack&#39;</span><span class="p">,</span> <span class="s">&#39;python&#39;</span><span class="p">)</span>
<span class="c"># output should be &quot;True&quot;</span>
<span class="n">redis_db</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="c"># now we have one key so the output will be &quot;[b&#39;full stack&#39;]&quot;</span>
<span class="n">redis_db</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;full stack&#39;</span><span class="p">)</span>
<span class="c"># output is &quot;b&#39;python&#39;&quot;, the key and value still exist in Redis</span>
<span class="n">redis_db</span><span class="o">.</span><span class="n">incr</span><span class="p">(</span><span class="s">&#39;twilio&#39;</span><span class="p">)</span>
<span class="c"># output is &quot;1&quot;, we just incremented even though the key did not</span>
<span class="c"># previously exist</span>
<span class="n">redis_db</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;twilio&#39;</span><span class="p">)</span>
<span class="c"># output is &quot;b&#39;1&#39;&quot; again, since we just obtained the value from</span>
<span class="c"># the existing key</span>
<span class="n">redis_db</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s">&#39;twilio&#39;</span><span class="p">)</span>
<span class="c"># output is &quot;1&quot; because the command was successful</span>
<span class="n">redis_db</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;twilio&#39;</span><span class="p">)</span>
<span class="c"># nothing is returned because the key and value no longer exist</span>
</pre></div>
<p>That is a quick introduction to some commonly-used Redis commands
invoked by their Python bindings through the redis-py library. Take a look
at the
<a href="https://redis-py.readthedocs.io/en/latest/">redis-py official documentation</a>
to learn more about the extensive command list you can use to create,
read, modify and delete keys and values in Redis.</p>
<p>Questions? Tweet <a href="https://twitter.com/fullstackpython">@fullstackpython</a>
or post a message on the
<a href="https://www.facebook.com/fullstackpython">Full Stack Python Facebook page</a>.
Something wrong with this post? Fork
<a href="https://github.com/makaimc/fullstackpython.com/blob/gh-pages/source/content/posts/160516-install-redis-use-python-3-ubuntu-1604.markdown">this page's source on GitHub</a>.</p></summary></entry><entry><title>How to Send MMS Picture Messages with Python</title><link href="http://www.fullstackpython.com/send-mms-picture-messages-python.html" rel="alternate"></link><updated>2016-05-15T00:00:00-04:00</updated><author><name>Matt Makai</name></author><id>tag:www.fullstackpython.com,2016-05-15:send-mms-picture-messages-python.html</id><summary type="html"><p>Multimedia Message Service (MMS) picture and video messages are a common
extension to the Short Message Service (SMS) system for sending text
messages. Using a
<a href="/application-programming-interfaces.html">web application programming interface (API)</a>
with Python makes it easy to send MMS messages from a web application or
script. In this short tutorial we'll learn how to add MMS sending capability
to a new or existing Python application.</p>
<h2>Tools We Need</h2>
<p>Either <a href="/python-2-or-3.html">Python 2 or 3</a> works for the code in this
tutorial. Just make sure you have one of those two versions installed on
your system by going to the terminal and typing <code>python --version</code>.
The other dependencies for this tutorial include:</p>
<ul>
<li><a href="https://www.python.org/">Python</a> version <a href="/python-2-or-3.html">2 or 3</a></li>
<li><a href="https://pip.pypa.io/en/stable/">pip</a> and
<a href="https://virtualenv.pypa.io/en/latest/">virtualenv</a> to handle one
<a href="/application-dependencies.html">application dependency</a></li>
<li>A free <a href="https://www.twilio.com/try-twilio">Twilio account</a> to use their
<a href="https://www.twilio.com/docs/api/rest/sending-messages">MMS web API</a></li>
<li><a href="https://pypi.python.org/pypi/twilio">Twilio Python helper library</a></li>
</ul>
<p>If you are unsure of how to get pip and virtualenv installed, take a look
at the first few steps of the
<a href="/blog/python-3-flask-green-unicorn-ubuntu-1604-xenial-xerus.html">how to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS</a>
guide.</p>
<h2>Twilio Web API</h2>
<p>Our simple Python example application will use the Twilio web API to send
picture messages.
Go to the Twilio website
<a href="https://www.twilio.com/try-twilio">sign up for a free trial account</a>. If
you already have a Twilio account (and you should because it makes it easy to
add almost any type of communications to applications!) then sign into
your existing account.</p>
<p><img src="/source/static/img/160515-mms-python/try-twilio.png" width="100%" class="technical-diagram img-rounded"></p>
<p>In trial mode Twilio can send MMS to a validated phone number associated with
the account. When you're ready to send MMS messages to any phone in any
country then you will have to upgrade your account.</p>
<p>After signing up for a Twilio account, you will receive your own phone
number that'll be used to send messages. That phone number can send outbound
MMS messages without any configuration. It can also receive messages but
that requires
<a href="https://www.twilio.com/docs/quickstart/python/sms/hello-monkey">modifying the Request URL webhook</a>
in the phone number details screen.</p>
<h2>Installing Our Dependency</h2>
<p>We'll use the <a href="https://pypi.python.org/pypi/twilio">twilio helper library</a>
as a dependency for our Python code. The helper library can be installed
via the <code>pip</code> command, which pulls the code from
<a href="https://pypi.python.org/pypi">PyPI</a> into our local virtualenv. In this
tutorial we'll call our virtualenv <code>pymms</code> but you can name it whatever
you want for your application.</p>
<p>We have to create the virtualenv before using it. In your terminal enter:</p>
<div class="highlight"><pre>virtualenv pymms
</pre></div>
<p>If you need to install virtualenv take a look at the
<a href="/blog/python-3-django-gunicorn-ubuntu-1604-xenial-xerus.html">how to set up Python 3, Django and Green Unicorn on Ubuntu 16.04 LTS</a>
guide.</p>
<p>Activate the virtualenv with the <code>source</code> command.</p>
<div class="highlight"><pre>source pymms/bin/activate
</pre></div>
<p>The command prompt will change to look like this after it is activated:</p>
<p><img src="/source/static/img/160515-mms-python/activate-virtualenv.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Now install the Twilio Python helper library.</p>
<div class="highlight"><pre>pip install twilio
</pre></div>
<p>Once the helper library installs we can use it in our Python code.</p>
<h2>Sending MMS From Python</h2>
<p>Launch the the Python interpreter by executing the <code>python</code> command in
your terminal. You can also create a new file named <code>send_mms.py</code> if you
want to re-use the code after we give it a try. </p>
<p>We need to grab our account credentials from the Twilio Console to connect
our Python code to our Twilio account. Go to the
<a href="https://www.twilio.com/console">Twilio Console</a> and copy the Account SID
and Authentication Token into your Python code.</p>
<p><img src="/source/static/img/160515-mms-python/console-tokens.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Enter the following code into the interpreter or into the new Python file.</p>
<div class="highlight"><pre><span class="c"># we import the Twilio client from the dependency we just installed</span>
<span class="kn">from</span> <span class="nn">twilio.rest</span> <span class="kn">import</span> <span class="n">TwilioRestClient</span>
<span class="c"># the following line needs your Twilio Account SID and Auth Token</span>
<span class="n">client</span> <span class="o">=</span> <span class="n">TwilioRestClient</span><span class="p">(</span><span class="s">&quot;ACxxxxxxxxxxxxxx&quot;</span><span class="p">,</span> <span class="s">&quot;zzzzzzzzzzzzz&quot;</span><span class="p">)</span>
<span class="c"># this is the URL to an image file we&#39;re going to send in the MMS</span>
<span class="n">media</span> <span class="o">=</span> <span class="s">&quot;http://www.mattmakai.com/source/static/img/work/fsp-logo.png&quot;</span>
<span class="c"># change the &quot;from_&quot; number to your Twilio number and the &quot;to&quot; number</span>
<span class="c"># to the phone number you signed up for Twilio with, or upgrade your</span>
<span class="c"># account to send MMS to any phone number that MMS is available</span>
<span class="n">client</span><span class="o">.</span><span class="n">messages</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">to</span><span class="o">=</span><span class="s">&quot;+19732644152&quot;</span><span class="p">,</span> <span class="n">from_</span><span class="o">=</span><span class="s">&quot;+12023358536&quot;</span><span class="p">,</span>
<span class="n">body</span><span class="o">=</span><span class="s">&quot;MMS via Python? Nice!&quot;</span><span class="p">,</span> <span class="n">media_url</span><span class="o">=</span><span class="n">media</span><span class="p">)</span>
</pre></div>
<p>All the lines above that start with <code>#</code> are comments to give you some
context for what each line is doing. After entering that code into the
interpreter or running the Python script with <code>python send_mms.py</code>
Twilio will send your MMS.</p>
<p>In a few seconds you should see a message appear on your phone - note that
MMS can take a little longer because your phone has to download the image.
I use an iPhone so here is what the message looked like when I received it:</p>
<p><img src="/source/static/img/160515-mms-python/mms-result.jpg" width="100%" class="technical-diagram img-rounded"></p>
<p>That is everything need to send MMS to a phone. Pretty awesome result for
a few lines of Python code, right? This code can be added to any Python
program to send outbound MMS.</p>
<p>One final note: keep your Twilio Auth Token secret otherwise anyone who
gets it will be able to send and receive messages through your account.</p>
<p>Questions? Contact me via Twitter
<a href="https://twitter.com/fullstackpython">@fullstackpython</a>
or <a href="https://twitter.com/mattmakai">@mattmakai</a>. I'm also on GitHub with
the username <a href="https://github.com/makaimc">makaimc</a>.</p></summary></entry><entry><title>Configuring Python 3, Bottle and Gunicorn for Development on Ubuntu 16.04 LTS</title><link href="http://www.fullstackpython.com/python-3-bottle-gunicorn-ubuntu-1604-xenial-xerus.html" rel="alternate"></link><updated>2016-05-13T00:00:00-04:00</updated><author><name>Matt Makai</name></author><id>tag:www.fullstackpython.com,2016-05-13:python-3-bottle-gunicorn-ubuntu-1604-xenial-xerus.html</id><summary type="html"><p>The Ubuntu 16.04 Long Term Support (LTS) Linux
<a href="/operating-systems.html">operating system</a> was released in April 2016.
This latest Ubuntu release is named "Xenial Xerus" and
it is the first Ubuntu release to include <a href="/python-2-or-3.html">Python 3</a>,
instead of Python 2.x, as the default Python installation.</p>
<p>We can quickly start a new <a href="/bottle.html">Bottle</a> web application project
and run it with <a href="/green-unicorn-gunicorn.html">Green Unicorn (Gunicorn)</a> on
Ubuntu 16.04.</p>
<h2>Tools We Need</h2>
<p>Our setup requires the Ubuntu 16.04 release along with a few other code
libraries. Don't install these tools just yet since we'll get to them as
we go through the walkthrough. Our requirements and their current versions
as of May 13, 2016 are:</p>
<ul>
<li><a href="http://releases.ubuntu.com/16.04/">Ubuntu 16.04 LTS (Xenial Xerus)</a></li>
<li><a href="/why-use-python.html">Python</a> version
<a href="https://docs.python.org/3/whatsnew/3.5.html">3.5</a>
(default in Ubuntu 16.04)</li>
<li><a href="/bottle.html">Botle</a> web framework version
<a href="http://bottlepy.org/docs/0.12/">0.12</a></li>
<li><a href="/green-unicorn-gunicorn.html">Green Unicorn (Gunicorn)</a> version
<a href="http://docs.gunicorn.org/en/stable/news.html">19.5</a></li>
</ul>
<p>If you are developing on Mac OS X or Windows, make sure to use
virtualization software such
as <a href="https://www.parallels.com/products/desktop/">Parallels</a> or
<a href="https://www.virtualbox.org/wiki/Downloads">VirtualBox</a> with the
<a href="http://releases.ubuntu.com/16.04/">Ubuntu .iso file</a>. Either the amd64 or
i386 version of 16.04 is fine. I use the amd64 version for my own local
development.</p>
<p>A desktop screen like this one appears when you boot up Ubuntu.</p>
<p><img src="/source/static/img/160513-ubuntu-bottle-gunicorn/ubuntu-desktop.jpg" width="100%" class="technical-diagram img-rounded"></p>
<p>Open a terminal window to install the system packages.</p>
<h2>System Packages</h2>
<p>We can see the python3 system version Ubuntu comes with and where its
executable is stored using these commands.</p>
<div class="highlight"><pre>python3 --version
which python3
</pre></div>
<p><img src="/source/static/img/160513-ubuntu-bottle-gunicorn/which-python.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Our Ubuntu installation requires a few system packages. We will get prompted
for the superuser password because restricted system access is needed
to install packages through
<a href="https://en.wikipedia.org/wiki/Advanced_Packaging_Tool">apt</a>.</p>
<div class="highlight"><pre>sudo apt-get install virtualenv python-pip python3-dev
</pre></div>
<p><img src="/source/static/img/160513-ubuntu-bottle-gunicorn/install-packages.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Enter <code>y</code> to let the system package installation process do its job.</p>
<p><img src="/source/static/img/160513-ubuntu-bottle-gunicorn/packages-installed.png" width="100%" class="technical-diagram img-rounded"></p>
<p>The packages we need are now installed. We can continue on to install our
Python-specific dependencies.</p>
<h2>Virtualenv</h2>
<p>In the previous section, <a href="https://virtualenv.pypa.io/en/latest/">virtualenv</a>
and <a href="https://pypi.python.org/pypi/pip">pip</a> were installed to handle our
<a href="/application-dependencies.html">application dependencies</a>.
We can now use them to download and install Bottle and Gunicorn.</p>
<p>Create a directory for the virtualenvs. Then create a new virtualenv.</p>
<div class="highlight"><pre># the tilde &quot;~&quot; specifies the user&#39;s home directory, like /home/matt
cd ~
mkdir venvs
# specify the system python3 installation
virtualenv --python=/usr/bin/python3 venvs/bottleproj
</pre></div>
<p>Activate the virtualenv.</p>
<div class="highlight"><pre>source ~/venvs/bottleproj/bin/activate
</pre></div>
<p>Our prompt will change after we properly activate the virtualenv.</p>
<p><img src="/source/static/img/160513-ubuntu-bottle-gunicorn/venv-activated.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Our virtualenv is now activated with Python 3. We can install whatever
dependencies we want, in our case Bottle and Gunicorn. </p>
<h2>Bottle and Gunicorn</h2>
<p>We can now install Bottle and Green Unicorn via the <code>pip</code> command.</p>
<div class="highlight"><pre>pip install bottle gunicorn
</pre></div>
<p>No errors like we see in the following screenshot is a good sign.</p>
<p><img src="/source/static/img/160513-ubuntu-bottle-gunicorn/good-sign.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Use the <code>mkdir</code> command to create a new directory to keep our Bottle
project then use the <code>cd</code> (change directory) command to move into the
new folder.</p>
<div class="highlight"><pre>mkdir ~/bottleproj
cd ~/bottleproj
</pre></div>
<p>Create a new file named <code>app.py</code> within our <code>bottleproj</code> directory so
we can test to make sure Bottle is working properly. I prefer to use
<a href="/vim.html">Vim</a> but <a href="/emacs.html">Emacs</a> and other
<a href="/development-environments.html">development environments</a> work great as
well.</p>
<p>Within the new <code>app.py</code> file write the following code.</p>
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">bottle</span>
<span class="kn">from</span> <span class="nn">bottle</span> <span class="kn">import</span> <span class="n">route</span><span class="p">,</span> <span class="n">run</span><span class="p">,</span> <span class="n">Response</span>
<span class="c"># a basic URL route to test whether Bottle is responding properly</span>
<span class="nd">@route</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">():</span>
<span class="k">return</span> <span class="n">Response</span><span class="p">(</span><span class="s">&quot;It works!&quot;</span><span class="p">)</span>
<span class="c"># these two lines are only used for python app.py</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
<span class="n">run</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="s">&#39;0.0.0.0&#39;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">8000</span><span class="p">,</span> <span class="n">debug</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">reloader</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="c"># this is the hook for Gunicorn to run Bottle</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">bottle</span><span class="o">.</span><span class="n">default_app</span><span class="p">()</span>
</pre></div>
<p>We could run our app with the Bottle development server using the
<code>python app.py</code> command. Let's instead run our Bottle app with
Gunicorn.</p>
<div class="highlight"><pre>gunicorn -w 2 app:app
</pre></div>
<p><img src="/source/static/img/160513-ubuntu-bottle-gunicorn/gunicorn-run.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Sweet, we can bring up our shell Bottle app in the web browser at
the <code>localhost:8000</code> or <code>127.0.0.1:8000</code> address.</p>
<p><img src="/source/static/img/160513-ubuntu-bottle-gunicorn/it-works.jpg" width="100%" class="technical-diagram img-rounded"></p>
<p>Time to develop a full-fledged web application with <a href="/bottle.html">Bottle</a>!</p>
<h2>Ready for Development</h2>
<p>Now you have a simple setup to develop Bottle web apps using Gunicorn as
the <a href="/wsgi-servers.html">WSGI server</a> on Ubuntu 16.04. If you need a
full step-by-step tutorial to deploy your Python web application to a
production environment, check out the
<a href="http://www.deploypython.com/">Full Stack Python Guide to Deployments book</a>.</p>
<p>To decide what to do next with your Python project, check out the
<a href="/table-of-contents.html">Full Stack Python table of contents</a> page.</p></summary></entry><entry><title>How to Send SMS Text Messages with Python</title><link href="http://www.fullstackpython.com/send-sms-text-messages-python.html" rel="alternate"></link><updated>2016-05-11T00:00:00-04:00</updated><author><name>Matt Makai</name></author><id>tag:www.fullstackpython.com,2016-05-11:send-sms-text-messages-python.html</id><summary type="html"><p>Short Message Service (SMS) text messages are ubiquitous for communication
all over the world. It is easy to send SMS text messages from a
<a href="/why-use-python.html">Python</a> application using a
<a href="/application-programming-interfaces.html">web application programming interface (API)</a>.
Let's take a look at the tools we need to quickly add SMS capability to our
Python apps.</p>
<h2>Tools We Need</h2>
<p>This guide works with both Python 2 and 3, so make sure you have one of those
two versions installed.</p>
<ul>
<li>Either <a href="/python-2-or-3.html">Python 2 or 3</a></li>
<li><a href="https://pip.pypa.io/en/stable/">pip</a> and
<a href="https://virtualenv.pypa.io/en/latest/">virtualenv</a> to handle
<a href="/application-dependencies.html">application dependencies</a></li>
<li>A free <a href="https://www.twilio.com/try-twilio">Twilio account</a> to use their
<a href="https://www.twilio.com/docs/api/rest/sending-messages">SMS web API</a></li>
<li>Open source
<a href="https://pypi.python.org/pypi/twilio">Twilio Python helper library</a></li>
</ul>
<p>If you need assistance getting pip and virtualenv installed, check out the
first few steps of the
<a href="/blog/python-3-flask-green-unicorn-ubuntu-1604-xenial-xerus.html">how to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS</a>
guide that'll show how to install system packages for those tools.</p>
<h2>Using a Web API</h2>
<p>We're going to use a web API to make sending SMS easier and more reliable.
Head to the
<a href="https://www.twilio.com/try-twilio">Twilio website and sign up for a free trial account</a>. If you already have a Twilio account (and you should - it's awesome
for more than just sending text messages!) then sign into your existing
account.</p>
<p><img src="/source/static/img/160511-send-sms-python/try-twilio.png" width="100%" class="technical-diagram img-rounded"></p>
<p>The Twilio trial account allows you to send text messages to your own
validated phone number. When you want to send SMS to any phone number in
your country or other countries then you can upgrade your account to send
messages for fractions of a cent.</p>
<p>After signing up, you will get a free phone number in your country. We can
use that phone number without any configuration to send outbound text
messsages. You can also receive text messages but that requires changing
the Request URL webhook in the phone number configuration screen - we'll
cover that in a future blog post.</p>
<h2>Installing Our Dependency</h2>
<p>Our code will use a helper library to make it easier to send text messages
from Python. We are going to install the helper library from
<a href="https://pypi.python.org/pypi">PyPI</a> into a virtualenv. First we need to
create the virtualenv. In your terminal use the following command to create
a new virtualenv. If you need to install virtualenv take a look at the
<a href="/blog/python-3-flask-green-unicorn-ubuntu-1604-xenial-xerus.html">how to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS</a>
guide.</p>
<div class="highlight"><pre>virtualenv sendsms
</pre></div>
<p>Activate the virtualenv.</p>
<div class="highlight"><pre>source sendsms/bin/activate
</pre></div>
<p>The command prompt will change after we properly activate the virtualenv
to something like this:</p>
<p><img src="/source/static/img/160511-send-sms-python/activate-virtualenv.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Now install the Twilio Python helper library.</p>
<div class="highlight"><pre>pip install twilio
</pre></div>
<p>The helper library is now installed and we can use it with the Python code
we create and execute.</p>
<h2>Sending SMS From Python</h2>
<p>Fire up the Python interpreter in the terminal using the <code>python</code> command,
or create a new file named <code>send_sms.py</code>. </p>
<p>We need to grab our account credentials from the Twilio Console to connect
our Python code to our Twilio account. Go to the
<a href="https://www.twilio.com/console">Twilio Console</a> and copy the Account SID
and Authentication Token into your Python code.</p>
<p><img src="/source/static/img/160511-send-sms-python/console-tokens.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Enter the following code into the interpreter or into the new Python file.</p>
<div class="highlight"><pre><span class="c"># we import the Twilio client from the dependency we just installed</span>
<span class="kn">from</span> <span class="nn">twilio.rest</span> <span class="kn">import</span> <span class="n">TwilioRestClient</span>
<span class="c"># the following line needs your Twilio Account SID and Auth Token</span>
<span class="n">client</span> <span class="o">=</span> <span class="n">TwilioRestClient</span><span class="p">(</span><span class="s">&quot;ACxxxxxxxxxxxxxx&quot;</span><span class="p">,</span> <span class="s">&quot;zzzzzzzzzzzzz&quot;</span><span class="p">)</span>
<span class="c"># change the &quot;from_&quot; number to your Twilio number and the &quot;to&quot; number</span>
<span class="c"># to the phone number you signed up for Twilio with, or upgrade your</span>
<span class="c"># account to send SMS to any phone number</span>
<span class="n">client</span><span class="o">.</span><span class="n">messages</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">to</span><span class="o">=</span><span class="s">&quot;+19732644152&quot;</span><span class="p">,</span> <span class="n">from_</span><span class="o">=</span><span class="s">&quot;+12023358536&quot;</span><span class="p">,</span>
<span class="n">body</span><span class="o">=</span><span class="s">&quot;Hello from Python!&quot;</span><span class="p">)</span>
</pre></div>
<p>All the lines above that start with <code>#</code> are comments. Once you enter that
code into the interpreter or run the Python script using
<code>python send_sms.py</code> the SMS will be sent.</p>
<p>In a few seconds you should see a message appear on your phone. I'm on
iOS so here's how the text message I received looked.</p>
<p><img src="/source/static/img/160511-send-sms-python/hello-from-python.png" width="100%" class="technical-diagram img-rounded"></p>
<p>That's it! You can add this code to any Python code to send text messages.
Just keep your Auth Token secret as it'll allow anyone that has it to use
your account to send and receive messages.</p>
<p>Questions? Contact me via Twitter
<a href="https://twitter.com/fullstackpython">@fullstackpython</a>
or <a href="https://twitter.com/mattmakai">@mattmakai</a>. I'm also on GitHub with
the username <a href="https://github.com/makaimc">makaimc</a>.</p></summary></entry><entry><title>How to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS</title><link href="http://www.fullstackpython.com/python-3-flask-green-unicorn-ubuntu-1604-xenial-xerus.html" rel="alternate"></link><updated>2016-05-10T00:00:00-04:00</updated><author><name>Matt Makai</name></author><id>tag:www.fullstackpython.com,2016-05-10:python-3-flask-green-unicorn-ubuntu-1604-xenial-xerus.html</id><summary type="html"><p>Ubuntu's latest Long Term Support (LTS)
<a href="/operating-systems.html">operating system</a> was released last month, in
April 2016. The 16.04 update for Ubuntu is known as "Xenial Xerus" and
it's the first Ubuntu release to include <a href="/python-2-or-3.html">Python 3</a>
as the default Python installation.</p>
<p>We can use this new Ubuntu release along with Python version 3.5 to
start a new <a href="/flask.html">Flask</a> web application project and run it with
<a href="/green-unicorn-gunicorn.html">Green Unicorn (Gunicorn)</a>.</p>
<h2>Tools We'll Need</h2>
<p>We'll need the Ubuntu 16.04 release along with a few other libraries to
complete our project. You don't have to install these tools just yet,
we will get to them as we progress through the walkthrough. Our requirements
and their current versions as of May 10, 2016 are:</p>
<ul>
<li><a href="http://releases.ubuntu.com/16.04/">Ubuntu 16.04 LTS (Xenial Xerus)</a></li>
<li><a href="/why-use-python.html">Python</a> version
<a href="https://docs.python.org/3/whatsnew/3.5.html">3.5</a>
(default in Ubuntu 16.04)</li>
<li><a href="/flask.html">Flask</a> web framework version
<a href="http://flask.pocoo.org/docs/0.10/">0.10</a></li>
<li><a href="/green-unicorn-gunicorn.html">Green Unicorn (Gunicorn)</a> version
<a href="http://docs.gunicorn.org/en/stable/news.html">19.4</a></li>
</ul>
<p>If you're running on Mac OS X or Windows, use virtualization software such
as <a href="https://www.parallels.com/products/desktop/">Parallels</a> or
<a href="https://www.virtualbox.org/wiki/Downloads">VirtualBox</a> with the
<a href="http://releases.ubuntu.com/16.04/">Ubuntu .iso file</a>. Either the amd64 or
i386 version of 16.04 is fine. I'm using amd64 for development and testing
in this tutorial.</p>
<p>Once you boot up Ubuntu, you should see a screen like this one.</p>
<p><img src="/source/static/img/160510-ubuntu-flask-gunicorn/ubuntu-desktop.jpg" width="100%" class="technical-diagram img-rounded"></p>
<p>Open up a terminal window to proceed with the setup.</p>
<h2>System Packages</h2>
<p>We can see the python3 system version Ubuntu comes with and where its
executable is stored using these commands.</p>
<div class="highlight"><pre>python3 --version
which python3
</pre></div>
<p><img src="/source/static/img/160510-ubuntu-flask-gunicorn/which-python.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Our Ubuntu installation requires a few system packages. We will get prompted
for the superuser password because restricted system access is needed
to install packages through
<a href="https://en.wikipedia.org/wiki/Advanced_Packaging_Tool">apt</a>.</p>
<div class="highlight"><pre>sudo apt-get install virtualenv python-pip python3-dev
</pre></div>
<p><img src="/source/static/img/160510-ubuntu-flask-gunicorn/install-packages.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Enter <code>y</code> to let the system package installation process do its job.</p>
<p><img src="/source/static/img/160510-ubuntu-flask-gunicorn/packages-installed.png" width="100%" class="technical-diagram img-rounded"></p>
<p>The packages we need are now installed. We can continue on to install our
Python-specific dependencies.</p>
<h2>Virtualenv</h2>
<p>In the previous section, <a href="https://virtualenv.pypa.io/en/latest/">virtualenv</a>
and <a href="https://pypi.python.org/pypi/pip">pip</a> were installed to handle our
<a href="/application-dependencies.html">application dependencies</a>.
We can now use them to download and install Flask and Gunicorn.</p>
<p>Create a directory for the virtualenvs. Then create a new virtualenv.</p>
<div class="highlight"><pre># the tilde &quot;~&quot; specifies the user&#39;s home directory, like /home/matt
cd ~
mkdir venvs
# specify the system python3 installation
virtualenv --python=/usr/bin/python3 venvs/flaskproj
</pre></div>
<p>Activate the virtualenv.</p>
<div class="highlight"><pre>source ~/venvs/flaskproj/bin/activate
</pre></div>
<p>Our prompt will change after we properly activate the virtualenv.</p>
<p><img src="/source/static/img/160510-ubuntu-flask-gunicorn/venv-activated.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Our virtualenv is now activated with Python 3. We can install whatever
dependencies we want, in our case Flask and Gunicorn. </p>
<h2>Flask and Gunicorn</h2>
<p>We can finally install Flask and Green Unicorn via the pip command.</p>
<div class="highlight"><pre>pip install flask gunicorn
</pre></div>
<p>It's a good sign if we receive no errors like we see in the following
screenshot.</p>
<p><img src="/source/static/img/160510-ubuntu-flask-gunicorn/good-sign.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Create a new directory under our home directory that will store our
Flask project. Change directory into the new folder.</p>
<div class="highlight"><pre>mkdir ~/flaskproj
cd ~/flaskproj
</pre></div>
<p>Create a new file named <code>__init__.py</code> within our <code>flaskproj</code> directory so
we can test to make sure Flask is working properly. I prefer to use
<a href="/vim.html">Vim</a> but <a href="/emacs.html">Emacs</a> and other
<a href="/development-environments.html">development environments</a> work great as
well.</p>
<p>Within <code>__init__.py</code> write the following code.</p>
<div class="highlight"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">Response</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">():</span>
<span class="k">return</span> <span class="n">Response</span><span class="p">(</span><span class="s">&quot;It works!&quot;</span><span class="p">),</span> <span class="mi">200</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
<span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">debug</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
<p>We could run our app with the Flask development server using the
<code>python __init__.py</code> command. Instead run the Flask app with
Gunicorn. Go to the directory above the <code>flaskproj</code> folder, in our
case we can enter <code>cd ~</code> then use the <code>gunicorn</code> command:</p>
<div class="highlight"><pre>gunicorn flaskproj:app
</pre></div>
<p><img src="/source/static/img/160510-ubuntu-flask-gunicorn/gunicorn-run.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Sweet, we can bring up our shell Flask app in the web browser at
the <code>localhost:8000</code> or <code>127.0.0.1:8000</code> address.</p>
<p><img src="/source/static/img/160510-ubuntu-flask-gunicorn/it-works.jpg" width="100%" class="technical-diagram img-rounded"></p>
<p>Now ready for some real <a href="/flask.html">Flask</a> development!</p>
<h2>Ready for Development</h2>
<p>That's a simple setup for developing with Flask and Gunicorn on
Ubuntu 16.04. If you need an in-depth step-by-step tutorial to
deploy your <a href="/wsgi-servers.html">WSGI-powered web application</a> to a
production environment, check out the
<a href="http://www.deploypython.com/">Full Stack Python Guide to Deployments book</a>.</p>
<p>To determine what to code next for your Python project, read the topics
found on the <a href="/table-of-contents.html">table of contents</a> page.</p>
<p>Questions? Contact me via Twitter
<a href="https://twitter.com/fullstackpython">@fullstackpython</a>
or <a href="https://twitter.com/mattmakai">@mattmakai</a>. I'm also on GitHub with
the username <a href="https://github.com/makaimc">makaimc</a>.</p></summary></entry><entry><title>Setting up Python 3, Django and Gunicorn on Ubuntu 16.04 LTS</title><link href="http://www.fullstackpython.com/python-3-django-gunicorn-ubuntu-1604-xenial-xerus.html" rel="alternate"></link><updated>2016-05-09T00:00:00-04:00</updated><author><name>Matt Makai</name></author><id>tag:www.fullstackpython.com,2016-05-09:python-3-django-gunicorn-ubuntu-1604-xenial-xerus.html</id><summary type="html"><p>Ubuntu released the newest Long Term Support (LTS) version of its
<a href="/operating-systems.html">operating system</a> in April 2016. The update
brings Ubuntu to version 16.04 and its latest code name is
"Xenial Xerus". 16.04 is the first Ubuntu release to include
<a href="/python-2-or-3.html">Python 3</a> as the default Python installation.</p>
<p>Let's use this newest Ubuntu release along with Python version 3.5 to
start a new <a href="/django.html">Django</a> web application project and run it with
<a href="/green-unicorn-gunicorn.html">Green Unicorn (Gunicorn)</a>.</p>
<h2>Tools We'll Need</h2>
<p>We will need a few tools to complete our project. Don't worry about
installing these just yet as we'll get to them as we progress through the
tutorial. The tools and their current versions as of May 9, 2016 are:</p>
<ul>
<li><a href="http://releases.ubuntu.com/16.04/">Ubuntu 16.04 LTS (Xenial Xerus)</a></li>
<li><a href="/why-use-python.html">Python</a> version
<a href="https://docs.python.org/3/whatsnew/3.5.html">3.5</a>
(default in Ubuntu 16.04)</li>
<li><a href="/django.html">Django</a> web framework version
<a href="https://docs.djangoproject.com/en/1.9/releases/1.9/">1.9.6</a></li>
<li><a href="/green-unicorn-gunicorn.html">Green Unicorn (Gunicorn)</a> version
<a href="http://docs.gunicorn.org/en/stable/news.html">19.4</a></li>
</ul>
<p>If you're running on Mac OS X or Windows, use virtualization software such
as <a href="https://www.parallels.com/products/desktop/">Parallels</a>
(this is what I use, but it's Mac OS X-only) or
<a href="https://www.virtualbox.org/wiki/Downloads">VirtualBox</a> with the
<a href="http://releases.ubuntu.com/16.04/">Ubuntu .iso file</a>. Either the amd64 or
i386 version of 16.04 is fine, but I use amd64 for development and testing
in this blog post.</p>
<p>When we boot up for the first time, we should see a desktop screen like this
one.</p>
<p><img src="/source/static/img/160509-ubuntu-django-gunicorn/ubuntu-desktop.jpg" width="100%" class="technical-diagram img-rounded"></p>
<p>Open up terminal to proceed with the setup.</p>
<h2>System Packages</h2>
<p>We can see the python3 version Ubuntu comes with, as well as where its
executable is stored.</p>
<div class="highlight"><pre>python3 --version
which python3
</pre></div>
<p><img src="/source/static/img/160509-ubuntu-django-gunicorn/which-python.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Our Ubuntu installation first needs system packages for Python development.
You'll be prompted for your superuser password because restricted system
access is required to install packages through apt.</p>
<div class="highlight"><pre>sudo apt-get install virtualenv python-pip python3-dev
</pre></div>
<p><img src="/source/static/img/160509-ubuntu-django-gunicorn/install-packages.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Enter <code>y</code> and let the system package installation process run.</p>
<p><img src="/source/static/img/160509-ubuntu-django-gunicorn/packages-installed.png" width="100%" class="technical-diagram img-rounded"></p>
<p>The basic system packages we need are now installed so we can proceed to
our Python-specific dependencies.</p>
<h2>Virtualenv</h2>
<p>Virtualenv and pip for isolating and handling
<a href="/application-dependencies.html">application dependencies</a> were just
installed via system packages so we can now use them to obtain Django and
Gunicorn.</p>
<p>Create a directory to store virtualenvs then put a new virtualenv in it.</p>
<div class="highlight"><pre># the tilde &quot;~&quot; specifies the user&#39;s home directory, like /home/matt
cd ~
mkdir venvs
# specify the system python3 installation
virtualenv --python=/usr/bin/python3 venvs/djproject
</pre></div>
<p>Activate the virtualenv.</p>
<div class="highlight"><pre>source ~/venvs/djproject/bin/activate
</pre></div>
<p>We should see our prompt change so that we know the virtualenv is properly
activated.</p>
<p><img src="/source/static/img/160509-ubuntu-django-gunicorn/venv-activated.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Our virtualenv with Python 3 is activated so we can install whatever
dependencies we want, such as Django and Gunicorn. </p>
<h2>Django and Gunicorn</h2>
<p>Time to install Django and Green Unicorn into our virtualenv.</p>
<div class="highlight"><pre>pip install django gunicorn
</pre></div>
<p>No errors is a good sign everything worked for us.</p>
<p><img src="/source/static/img/160509-ubuntu-django-gunicorn/good-sign.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Create a new Django project named <code>djproject</code>, or whatever you want to name
your project. Then change into the directory for the new project.</p>
<div class="highlight"><pre>django-admin startproject djproject
cd djproject
</pre></div>
<p>We could run Django with the development server using the
<code>python manage.py runserver</code> command. However, start Django up with
Gunicorn instead.</p>
<div class="highlight"><pre>gunicorn djproject.wsgi
</pre></div>
<p><img src="/source/static/img/160509-ubuntu-django-gunicorn/gunicorn-run.png" width="100%" class="technical-diagram img-rounded"></p>
<p>Awesome, now we can bring up our shell project in the web browser at
the <code>localhost:8000</code> or <code>127.0.0.1:8000</code> address.</p>
<p><img src="/source/static/img/160509-ubuntu-django-gunicorn/it-worked.jpg" width="100%" class="technical-diagram img-rounded"></p>
<p>Ready for development!</p>
<h2>Ready for Development</h2>
<p>Those are the basics for starting development with Django and Gunicorn on
Ubuntu 16.04. If you need an even more in-depth step-by-step tutorial to
deploy your Python web application to a production environment, check out the
<a href="http://www.deploypython.com/">Full Stack Python Guide to Deployments book</a>.</p>
<p>To figure out what to do next for your Python project, read the topics
found on the <a href="/table-of-contents.html">table of contents</a> page.</p>
<p>Questions? Contact me via Twitter
<a href="https://twitter.com/fullstackpython">@fullstackpython</a>
or <a href="https://twitter.com/mattmakai">@mattmakai</a>. I'm also on GitHub with
the username <a href="https://github.com/makaimc">makaimc</a>.</p></summary></entry><entry><title>The Full Stack Python Blog</title><link href="http://www.fullstackpython.com/full-stack-python-blog.html" rel="alternate"></link><updated>2016-05-08T00:00:00-04:00</updated><author><name>Matt Makai</name></author><id>tag:www.fullstackpython.com,2016-05-08:full-stack-python-blog.html</id><summary type="html"><p>Full Stack Python began way back in December 2012 when I started writing
the initial <a href="/deployment.html">deployment</a>, <a href="/servers.html">server</a>,
operating system, web server and WSGI server pages. Since then, the pages
have expanded out into a
<a href="/table-of-contents.html">boatload of other areas</a>
including subjects outside the deployment topics I originally started the
site to explain.</p>
<p>Frequently though I wanted to write a Python walkthrough that was not a
good fit for the page format I use for each topic. Many of those walkthroughs
became <a href="https://www.twilio.com/blog/author/mmakai">Twilio blog posts</a>
but not all of them were quite the right fit on there. I'll still be writing
plenty more Twilio tutorials, but this <a href="/blog.html">Full Stack Python blog</a>
is the spot for technical posts that fall outside the Twilio domain.</p>
<p>Let me know what you think and what tutorials you'd like to see in the future.
Hit me up on Twitter <a href="https://twitter.com/fullstackpython">@fullstackpython</a>
or <a href="https://twitter.com/mattmakai">@mattmakai</a>.</p></summary></entry></feed>