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.
First, assume that Pyenv is installed and remove it. If you don't have Pyenv installed, start by installing Python3.
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
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
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
From Python 3.3 ?, pyvenv (venv) that manages the virtual environment is installed. This time, we will use it to 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.
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.
To get out of the virtual environment
deactivate
will do. The prompt returns to normal.
Now, let's install Django while in the disguise environment.
. myenv/bin/activate
pip install djange
Django is installed.
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.
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