Until you can step in PyCharm + Django + virtual environment (pyvenv + Python3) (mac)

I will write it assuming our employees who want to try it but cannot do it because the threshold is high.

The Django project name is tentatively "test-oauth-client". [^ 1]

[^ 1]: Was test-oauth-consumer a more appropriate name ...

PyCharm is a paid Python development environment.

1. Until you create a Python virtual environment

1-1. Installation of HomeBrew [^ 2]

http://brew.sh/index_ja.html From here.

[^ 2]: Until now, when I read "Home Brew", it was pointed out that "It's Home Blue". When I watch the YouTube video, it looks like "Home Blue".

1-2. Installing Python3

$ brew install python3

スクリーンショット 2016-05-23 17.13.42.png

When complete, the pyvenv-3.5 command will be installed with it.

1-3. Creating a virtual environment

We will use pyvenv-3.5, which was installed with Python3 earlier.

$ cd
$ mkdir .virtualenvs
$ pyvenv-3.5 .virtualenvs/test-oauth-client

A Python3.5 virtual environment is created in the home directory /.virtualenvs/test-oauth-client. The name of your virtual environment doesn't have to match the Django project name, but it's easier to see.

1-4. Try entering the virtual environment

$ . .virtualenvs/test-oauth-client/bin/activate

↑ It is a dot command. The same is true for source .virtualenvs / test-oauth-client / bin / activate.

スクリーンショット 2016-05-23 17.19.13.png

(Test-oauth-client) is added to the prompt. This means that you have entered a virtual environment.

$ python --version
Python 3.5.1
$ /usr/bin/env python --version
Python 3.5.1
$ which python
/Users/ytyng/.virtualenvs/test-oauth-client/bin/python

The default Python location changes only while you are in the virtual environment.

To get out of the virtual environment

$ deacitvate

It may be useful to have a Python library called "** virtualenvwrapper **" to enter the virtual environment (not mentioned here).

2. Until you create a Django project

2-1. Enter the virtual environment

Activate it to install Django on your virtual environment.

$ cd
$ . .virtualenvs/test-oauth-client/bin/activate

2-2. Creating a directory

Create a test-oauth-client directory under a directory of your choice. This is the root of the repository.

$ cd workspace
$ mkdir test-oauth-client
$ cd test-oauth-client

2-3. Installing Django

You have Django installed to create the project directory with Django commands.

$ pip install django

2-3. startproject

Create a Django project in the directory you just saw. It's a good idea to move your Django project down one level from the repository root.

$ pwd
/Users/ytyng/workspace/test-oauth-client
$ django-admin.py startproject test_oauth_client

The test_oauth_client directory is created.

2-4. Run the Django project

$ cd test_oauth_client
$ ./manage.py

スクリーンショット 2016-05-23 17.37.02.png

Confirm that the subcommand list is displayed without any error.

$ ./manage.py migrate

The initial database is created. SQLite3 is used by default.

2-5. Run the Django project

It's a project that doesn't do anything at all, but let's start a test HTTP server for now.

$ ./manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
May 23, 2016 - 08:38:32
Django version 1.9.6, using settings 'test_oauth_client.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

If it is output like this, it is running.

2-6. Checking the operation with a browser

In your browser, try accessing http://127.0.0.1:8000/.

If you see "It worked!", Django is running.

スクリーンショット 2016-05-23 17.40.31.png

After confirming the operation, press Ctrl + c on the terminal to shut down the server.

3. Install and configure PyCharm

3-1. Installation

https://www.jetbrains.com/pycharm/ Please install from here. You can try it for 30 days.

3-2. Launch and project registration

Go to the repository root

$ cd ~/workspace/test-oauth-client/

Start PyCharm

$ charm .

This is the same. $ open -a PyCharm . You can also double-click the PyCharm icon to start it and open the directory from File → Open.

3-3. PyCharm setup

3-3-1. Project structure settings

First, open your preferences.

⌘+,

Enter "** structure **" in the search window and click "Project Structure" in the left pane.

You'll see Django's project root, ** test_oauth_client ** (one level below the repository root), click it to select it, and click ** Sources **.

A directory will be added to "Source Folders" in the right pane, so click "OK".

スクリーンショット 2016-05-23 18.10.02.png

3-3-2. Specifying a Python virtual environment

Open your preferences. ⌘ +,

Enter "** interpreter **" in the search window.

"Project Interpreter" will appear in the search results, so click it.

Click the "Project Interpreter" pull-down in the right pane to open it, and click "Show All" at the bottom.

スクリーンショット 2016-05-23 17.50.25.png

The Project Interpreters dialog will be displayed. Click the "+" at the bottom left and select "Add Local".

スクリーンショット 2016-05-23 17.52.01.png

A file selection dialog will be displayed. Click the home icon on the upper left once.

Then, find the **. Virtualenvs / test-oauth-client ** you registered earlier and open it. In addition, select the ** bin / python ** symbolic link in it and click "OK".

スクリーンショット 2016-05-23 17.54.21.png

In addition, click "OK" → Return to the Preference window, so after confirming that the virtual environment was selected, click "OK".

In the background, a rescan of the project structure begins.

3-3-3. Specifying the Django environment

Open your preferences. ⌘ +,

Enter "** django **" in the search window.

"Django" in Languages & Frameworks will be searched, so click to select it.

Check "Enable Django Support".

Specify test_oauth_client (one level below the repository root) for Django project root

Specify settings.py in settings. Click "OK"

スクリーンショット 2016-05-23 18.05.25.png

3-3-4. Boot configuration settings

Click the [▼] pull-down in the upper right corner of PyCharm, and click "Edit Configurations ...".

スクリーンショット 2016-05-23 18.13.47.png

The Run / Debug Configurations dialog opens, click + in the upper right corner and select "Django server"

スクリーンショット 2016-05-23 18.14.29.png

Name: can be arbitrary, but for the time being, set it to runserver and click OK.

Click the bug button to the right of the two pull-downs. It will be in debug execution mode.

スクリーンショット 2016-05-23 18.16.11.png

If you try to access http://127.0.0.1:8000/ with a browser, the demo page will be displayed.

3-4. Breakpoint setting

You are ready to debug in PyCharm.

Now, since the current project content is the default, there isn't even a program to set breakpoints, so let's add breakpoints in Django's code.

On PyCharm, press Shift four times and enter "** get_response **" in the search window. There are some get_responses that apply, but choose the one that says ** BaseHandler in ... **.

スクリーンショット 2016-05-23 18.33.44.png

The get_response method of the BaseHandler class is displayed, and you can set a breakpoint by clicking on the left side of the editor pane.

スクリーンショット 2016-05-23 18.37.51.png

Then reload the browser and you should be able to stop at the breakpoint and step through or see the variables in the debugger.

スクリーンショット 2016-05-23 18.38.37.png

Recommended Posts

Until you can step in PyCharm + Django + virtual environment (pyvenv + Python3) (mac)
[Django3] Display a web page in Django3 + WSL + Python virtual environment
Until you put Python in Docker
Install Django in a pipenv virtual environment
[Mac] Building a virtual environment for Python
Until you can use opencv with python
Ubuntu18.04.05 Creating a python virtual environment in LTS
Start Django in a virtual environment with Pipenv
Create a virtual environment with conda in Python
Until you run the changefinder sample in python
Create a Django project and application in a Python virtual environment and start the server
Think about building a Python 3 environment in a Mac environment
Work in a virtual environment with Python virtualenv.
Use jupyter-lab installed in python virtual environment (venv)
Japanese can be used with Python in Docker environment
Build a Python virtual environment using venv (Django + MySQL ①)
Until you insert data into a spreadsheet in Python
Until you run server Django in Visual Studio Code
Until you install TensorFlow-GPU with pip in Windows environment
Development environment in Python
Mac environment construction Python
Virtual environment with Python 3.6
Try running python in a Django environment created with pipenv
Until you can install your own Python library with pip
[Python] Anaconda environment construction (installation, startup, virtual environment, package management) Mac environment
If you write TinderBot in Python, she can do it
Until Python [Django] de Web service is released [Environment construction]
A note on how to load a virtual environment in PyCharm
How to develop in a virtual environment of Python [Memo]
After enabling the python virtual environment in the batch file, run the python file
[Python] PyCharm environment settings (installation, interpreter settings, addition of packages) Mac environment
Create a virtual environment for python on mac [Very easy]
VScode environment construction (on Mac) & graph display in Python (@browser)
Python + Anaconda + Pycharm environment construction
Python3 + Django ~ Mac ~ with Apache
Handle environment variables in Python
[Python] Insert ":" in MAC address
Python environment construction For Mac
Building a Python virtual environment
venv: Python virtual environment management
(Note) Django in Vagrant environment
python standard virtual environment venv
Building a Python virtual environment
If you know Python, you can make a web application with Django
[Understanding in the figure] Management of Python virtual environment by Pipenv
PATH when using ANACONDA virtual environment with Pycharm (as of Mac 2020/10/03)
Build a Python environment on your Mac with Anaconda and PyCharm
Try using virtualenv, which can build a virtual environment for Python
From installing Ansible to building a Python environment in Vagrant's virtual environment