Introduction to Python Django (2) Mac Edition

Explains how to build a development environment for Python and Django on Mac.

Xcode、Command Line Tools

When developing on macOS, even if you do not plan to develop an iPhone application It's a good idea to include Xcode and Command Line Tools.

Xcode is from the App Store icon. Command Line Tools

$ xcode-select --install

Install at.

Install Python3

Python2 is included in Mac OS X, but if you're new to learning, you should learn Python3.

HomeBrew installation

HomeBrew is a Mac package management tool.

If you have already introduced it, please skip it. If you are a MacPorts sect, please introduce Python 3 by yourself.

To install, open the terminal and execute the following command as per the official website.

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install Python3

Update HomeBrew to the latest View information on python3

$ brew update
$ brew info python3
python: stable 3.7.6 (bottled), HEAD

Please install the latest Python 3 at that time.

$ brew install python3

Recent brews now include 3 instead of Python 2 in brew install python.

Also, the latest version of Python at the moment is 3.8.1, but it doesn't seem to be included by default yet. It seems that you can force it with brew install [email protected], If you look it up with brew info [email protected],

[email protected] is keg-only, which means it was not symlinked into /usr/local, because this is an alternate version of another formula.

I get a disturbing message, so I haven't tried it. For now, continue with 3.7.6.


When dealing with Python in business, put anyenv, put pyenv, and perform Python version control, but it is not for beginners, so brew It is explained in. If you are interested, please check it out later.

Python 2 and 3 are compatible. Let's take a look at the version.

$ python -V
Python 2.7.16

$ python3 -V
Python 3.7.6

Installation of virtualenv and virtualenvwrapper

virtualenv is a tool for creating a virtual environment for Python. For each project

Build a fixed virtual environment.

virtualenvwrapper is a wrapper that makes it a little easier to create and delete virtual environments with virtualenv.

There is also a command to create a virtual environment called venv in the Python3 standard, but this one is used.

Put pip on python2 side

$ sudo easy_install pip

Insert virtualenv and virtualenvwrapper on Python2 side

$ sudo pip install virtualenv virtualenvwrapper --ignore-installed six

Contains virtualenv virtualenvwrapper that supports both Python2 and Python3.

virtualenv virtualenv wrapper setup procedure

    1. Create a directory for the virtual environment

    $ cd ~ $ mkdir ~/.virtualenvs

  1. Create a .bashrc file with a text editor or similar.

.bashrc


export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
    1. Add the following line to the last line of .bash_profile with a text editor.

.bash_profile


    :
    :
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi
  1. If the terminal stays open, import the edited .bash_profile.
$ source .bash_profile

Building a virtual environment

Create a virtual environment named env1

$ mkvirtualenv --no-site-package --python /usr/local/bin/python3 env1

--no-site-package is a specification that prevents inheritance of the base python site-package You can specify the interpreter to use with the -p or --python options, so specify python3.

Move to virtual environment

Immediately after creating a virtual environment with mkvirtualenv, the command prompt is as follows.

(env1) $

If (env1) is displayed, you are in a virtual environment. If you exit the terminal and reopen it, you can move to the virtual environment env1 with the following command.

$ workon env1

Other commands that handle virtual environments

Since env1 will be used after this, do not hit the following command, but remember it as knowledge.

View the list of current virtual environments

$ workon
env1

Switch to env1

$ workon env1

Disable virtual environment (return to global environment)

$ deactivate

Delete unnecessary virtual environment (after deactivating. All packages put in the virtual environment with pip will be deleted.)

$ deactivate
$ rmvirtualenv env1

Install Django

Django is also one of the Python modules. Install in the virtual environment env1.

This time, I'll install the Django version specified for this course.

$ workon env1
$ pip install django==3.0.2

Check the version

Check the packages installed in the virtual environment.

$ pip freeze -l
asgiref==3.2.3
Django==3.0.2
pytz==2019.3
sqlparse==0.3.0

Installation of IDE integrated development environment

You can use any text editor to write your Django code, but we recommend that you install PyCharm Professional (https://www.jetbrains.com/pycharm/) as your IDE.

Features of PyCharm

Recently, there is an option to use Visual Studio Code as a Python IDE. I won't explain it here, but if you like the Japanese one for free, you can choose it. · Python code completion -If you can specify where the Python interpreter of the Python virtual environment is, debug execution Etc. can be done in the same way as PyCharm.

Install PyCharm Professional

  1. Download the Professional version from the Download PyCharm page.
  2. Open the downloaded .dmg file.
  3. Drag PyCharm.app to your application folder as shown.

PyCharm.jpg

  1. After installation, let's start Application> PyCharm.app.

  2. If you are new to PyCharm, you can leave this screen as it is because there is no previous settings.

Complete_Installation.jpg

  1. Select the one for Evaluate for free for 30 days and press OK.

PyCharm_License_Activation.jpg

  1. Read carefully and press Accept.

License_Agreement_for_PyCharm_5_0_4.jpg

  1. Change only the theme. IDE theme: Darcula has a black background / white letters and I feel like writing code.

PyCharm_Initial_Configuration.jpg

  1. It's in the middle of booting, but if you change the theme settings, it looks like it's restarting. Press Yes.

This_should_not_be_shown.jpg

  1. Welcome to PyCharm

Welcome_to_PyCharm.jpg

Once you've come this far, let's move on.

Continue to Introduction to Python Django (3)

Recommended Posts

Introduction to Python Django (2) Mac Edition
Introduction to Python Django (2) Win
Introduction to python-Environmental preparation (mac edition)
Introduction to Python language
Introduction to OpenCV (python)-(2)
Python installation (Mac edition) (old)
Introduction to serial communication [Python]
Update python on Mac to 3.7-> 3.8
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
Introduction to Linux Commands ~ LS-DYNA Edition ~
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to TensorFlow --Hello World Edition
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Introduction to Deep Learning ~ Dropout Edition ~
Steps to install python3 on mac
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Python Tutorial] An Easy Introduction to Python
Update Python on Mac from 2 to 3
A story about trying to run multiple python versions (Mac edition)
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
Migrate Django applications running on Python 2.7 to Python 3.5
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Python] How to use class in Python?
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
Introduction to Discrete Event Simulation Using Python # 1
[Introduction to Udemy Python3 + Application] 13. Character method
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
A super introduction to Python bit operations
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Web-WF Python Tornado Part 3 (Introduction to Openpyexcel)
[Introduction to Udemy Python3 + Application] 21. Tuple type
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Introduction to Udemy Python3 + Application] 41. Input function
[Introduction to Python] Let's use foreach with Python
How to prepare Python development environment [Mac]
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Udemy Python3 + Application] 17. List operation
[Introduction to Udemy Python3 + Application] 65. Exception handling