[GO] Create a Python environment on Mac (2017/4)

I've written a note about using Python on a Mac.

It's getting a little old, so I'll update it. The Mac comes with 2.x Python from the beginning, but since the times are 3.x, set up 3.x.

In addition, since my main purpose of using Python is to use Django, I will write up to the point of using Django.

Remove pyenv

First, assume that Pyenv is installed and remove it. If you don't have Pyenv installed, start by installing Python3.

Uninstall

I installed it using brew, so uninstall it using brew. Delete pyenv and pyenv-virtualenv, and delete ~ / .pyenv where the files under each environment are saved.

brew uninstall --force pyenv
brew uninstall --force pyenv-virtualenv
rm -rf .pyenv/

If you have pyenv-pip-rehash installed, remove it as well. brew uninstall pyenv-pip-rehash

Remove related description in .bash_profile

Next, remove the description added to .bash_profile for pyenv to work effectively.

Please respond according to each environment.

-export PYENV_ROOT="${HOME}/.pyenv"
-export PATH=${PYENV_ROOT}/bin:$PATH
-eval "$(pyenv init -)"

When the deletion is complete, reload the .bash_profile and check the version. It seems to be the default Python version.

source .bash_profile
python -V
Python 2.7.13

Install Python3

Now, install Python 3.x series. Installation is done by brew.

If brew (homebrew) is not included, ask your Google teacher to install it.

brew install python3

Creating a virtual environment

From Python 3.3 ?, pyvenv (venv) that manages the virtual environment is installed. This time, we will use it to create a virtual environment.

Create a virtual environment

By executing the following command, a directory and files that manage your own environment called myenv will be created.

pyvenv myenv

In my environment, when the above command is executed,

WARNING: the pyenv script is deprecated in favour of `python3.6 -m venv`

Warning was displayed. Apparently, the pyvenv command is deprecated in the future,

python3 -m venv myenv

Use! ... apparently ... I will try it.

python3 -m venv myenv

The myenv directory has been created.

Run a virtual environment

Specify the created virtual environment folder and start the virtual environment.

. myenv/bin/activate
(myenv) :~ hoge$

If you do not start with>., a Permission error will occur.

Then, the prompt will be (Disguise environment name), and you can see that you are in the disguise environment. If you run python -V in this state, you can see that it is 3.x.

If you run pip in this state, the package will be reflected only in the virtual environment.

Exit the virtual environment

To get out of the virtual environment

deactivate

will do. The prompt returns to normal.

Install Django in a virtual environment

Now, let's install Django while in the disguise environment.

. myenv/bin/activate

pip install djange

Django is installed.

Create a Ddjango project

Now, create a project (directory) using the installed Django (command). Execute the following command.

django-admin.py startproject django_test

In this example, a project (folder) called django_test will be created.

Launch Django

Now, let's initialize the created project and run the project.

cd django_test
./manage.py #Check if it works
./manage.py migrate #Migrate. By default, sqlite is used as DB.
./manage.py runserver #Start development server

After starting, try accessing from a browser.

http://localhost:8000/

It seems to be working.

Recommended Posts

Create a Python environment on Mac (2017/4)
Create a python environment on your Mac
Building a Python environment on Mac
Create a python environment on centos
Create a Python environment
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Create a virtual environment for python on mac [Very easy]
Build a Python environment on Mac (Mountain Lion)
Build a Python development environment on your Mac
[Venv] Create a python virtual environment on Ubuntu
Create a Python execution environment on IBM i
Create a Python virtual development environment on Windows
Building a Python environment on Ubuntu
Build a machine learning Python environment on Mac OS
Create a virtual environment with Python!
Python environment construction memo on Mac
Create a comfortable Python 3 (Anaconda) development environment on windows
Create a Linux environment on Windows 10
Build a Python development environment on Mac OS X
python on mac
Build a Python environment on your Mac using pyenv
Environment construction of python3.8 on mac
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Create a decent shell and python environment on Windows
Build a python3 environment on CentOS7
Create a Python development environment on OS X Lion
Building a Python environment on a Mac and using Jupyter lab
Build a python data analysis environment on Mac (El Capitan)
Build a python environment on MacOS (Catallina)
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
[Python] Create a Batch environment using AWS-CDK
[Mac] Building a virtual environment for Python
Creating a python virtual environment on Windows
I installed Kivy on a Mac environment
Build Python environment with Anaconda on Mac
Create an OpenCV3 + python3 environment on OSX
Build a Python + OpenCV environment on Cloud9
Create a Python module
Install Python on Mac
Install Python 3 on Mac
Install Python 3.4 on Mac
Mac environment construction Python
Create a Python environment for professionals in VS Code on Windows
Build a Python environment on your Mac with Anaconda and PyCharm
[Mac] Create a Python3 execution environment from the fully initialized state
Create a Vim + Python test environment in 1 minute
How to create a Python virtual environment (venv)
Simply build a Python 3 execution environment on Windows
Create a virtual environment with conda in Python
Create a python3 build environment with Sublime Text3
Think about building a Python 3 environment in a Mac environment
Set up a Python development environment on Marvericks
Create a Mac app using py2app and Python3! !!
Building a Python environment on a Sakura VPS server
[Docker] Create a jupyterLab (python) environment in 3 minutes!
[Note] How to create a Mac development environment
Build a Python development environment on Raspberry Pi
Things to watch out for when creating a Python environment on a Mac
Create a Python development environment on Windows (Visual Studio Code remote WSL).
Steps to create a Python virtual environment with VS Code on Windows