Install Python venv --VSCode --GitHub integration environment on Mac

This is a record of installing Python venv --VSCode --GitHub integration environment on Mac. After trying to reinstall it, I generalized Original article.

The environment is MacOS Catalina, Python 3.8.7, VSCode 1.52.1.

1. Install Python

At the moment (January 2021), the latest version is Python 3.9, but since most of the machine learning libraries are finally compatible with Python 3.8, ** Python 3.9 is not recommended **. Install Python3.8 (the latest minor version).

Python 3.8.7's Big Sur support is "not yet fully". Therefore, it is better to wait for the next Python 3.8.8 (around February 2021) to update the OS to Big Sur.

There are various ways to install Python 3.8: (Note that Python 3.7 should be included in the xcode command line tool installation prior to homebrew)

(1) Installation by homebrew (2) GUI installation from Python official (3) GUI installation from Anaconda official (4) Installation by pyenv

This time, we recommend item 1 recommended by ** VS Code Official **. Also, in this article, it is assumed that pyenv and anaconda are not used as virtual environments, and the installation method using them will not be adopted. See also Section 4 for virtual environment selection.

If you simply use brew install python, Python 3.9 will be installed, so install it with brew install python @ 3.8. Follow the instructions of brew on the way. Finally, run echo'export PATH ="/usr/local/opt/[email protected]/bin: $ PATH "'>> ~/.zshrc in the brew instructions to enable the path and Restart the Command Prompt app.

From the command line, make sure that python with the version python3.8 works. Note that if you just type python, the python2 system will usually start up.

2. Create a GitHub remote repository

I'm trying to link the GitHub remote repository with the local repository on the Mac, but after trying various things, I think the "** Create remote first **" method is the easiest and correct.

If you haven't signed up (created an account) for GitHub, do so. It's a good idea to set up 2FA (two-step verification).

Create a new repository in the GitHub GUI. The following screen will appear. スクリーンショット 2021-01-03 22.55.11.png

Enter the repository name and click Create repository to create the repository. Please add options as you like. I usually specify a README file and a license (MIT). As for .gitignore, if you create it automatically here, a file with quite redundant contents will be created, so I will create it myself later.

Next, create a new develop branch from the repository screen.

You can create a develop branch by entering the new repository name develop in Switch branches/tags and clicking Create branch, referring to the screen shot below.

スクリーンショット 2021-01-03 22.57.12.png

Due to the 2020 BLM movement, the default branch name on GitHub has changed from master to main.

3. Creating a local repository and linking with a remote repository

Install the git command to work with GitHub. If you don't care about the latest version, git is included in the xcode command line tool, and if you have homebrew installed, it should already be installed in principle. Check with which git. If not, install it with brew install git, but it seems that you need to pass it in order to use it.

Complete the initial settings of the git command.

% git config --global user.name your Git account name
% git config --global user.email your email address

Install VS Code from the GUI. VS Code is frequently updated and its operations change frequently.

Launch VSCode and select Clone Repository ... at Start, guided on the initial welcome page. If you select "Duplicate from GitHub", you will go back and forth between the screens that give permission to link apps for duplication, and as a result, a list of your own repository names will appear, so select the repository name you created earlier.

On the new folder selection screen, select the folder ** one level above ** of the repository you want to create. Open the cloned repository.

Refer to the screen shot below, click the source control icon on the left side of the screen, click ... from the icon menu on the upper left side of the screen, and select the checkout destination ... By selecting origin/develop from the list that appears, the local copy of the develop branch will be in the current edit state.

スクリーンショット 2021-01-04 0.36.52.png

Hereafter, the area directly under the local repository name directory is called "project home".

4. Build a Python virtual environment with venv

Set up venv as a virtual environment.

To develop Python, you need to install various libraries, and the environment becomes more and more complicated. There are many occasions when you want to use different environment sets together or re-set up because you have destroyed the environment. Therefore, ** installation of virtual environment is mandatory in development environment **.

As a virtual environment, we strongly recommend that beginners do not use pyenv or anaconda. pyenv is often used in Japan, and many usage examples are hit when searching the Internet, and if you are self-taught, you just use it. However, many usage examples are remnants of the era when the official Python virtual environment was not easy to use. In addition, anaconda is intended for easy use that does not require additional installation of packages, so it is not suitable for development environments where you want to flexibly add packages. Today, much of the official or near-official content, such as qualification exams and training courses, is officially Python-enabled. Therefore, let's switch to another virtual environment if you like after you can use venv first.

Regarding the above allegations, we have created a separate Independent Article, so please refer to it as well.

In the project home, name python3.8 -m venv .venv. By executing the venv command with ** 3.8 and version, you can create the target version of python virtual environment even if multiple versions of Python are installed. ** .venv can be any other name, but a name that starts with. Is preferable so that it will be the target of the .gitignore you will create later. Various common libraries are installed in the venv environment, so if you do not hit .gitignore, it will be a big deal.

5. VS Code setup

Enable Python support with the customizations guided on the initial welcome page.

Next, make the venv environment created earlier the default execution environment for VS Code.

Open the project home with File-Open ... Next, in View-Command Palette ..., select the venv environment created earlier from "Python: Select Interpreter" on the command palette. If you select it here, VS Code will automatically activate venv according to various menu operations of VS Code.

Select a new terminal from the VS Code terminal menu and see if you get a ** venv enabled prompt **.

If you restart VSCode and run "New Terminal" with a file other than the python file selected, it may not be recognized as a Python environment and you may not be prompted with venv enabled. (Behavior of VS Code 1.52.1)

Also, run python -V, which python, pip -V in the terminal as shown below to check if the python version and pip version are for the venv environment.

(.venv)Project home% python -V
Python 3.8.7
(.venv)Project home e% which python
Project home/.venv/bin/python
(.venv)Project home% pip -V
pip 20.2.3 from project home/.venv/lib/python3.8/site-packages/pip (python 3.8)

Immediately after installing Python, pip may not be the latest version, so please update it with ** pip install -U pip under ** venv environment as appropriate. After updating, run pip -V to see if pip is in the venv environment.

In the meantime, if you're prompted to install something related to Python, follow the instructions.

Also, select flake8 with Python: Enable Linting on and Python: Select Linter on the command palette. You will be prompted to install flake8 while using it, so follow the instructions.

6. Setting commit exclusion files with .gitignore

Do not push work directories and files of various tools, work for development, and information that you do not want to publish (API key obtained by yourself, etc.) to the remote repository. The mechanism for that is .gitignore.

Create a .gitignore file in your project home using the VS Code GUI. The following is sufficient for the contents, and if you have any concerns individually, please add them.

.gitignore


.*
*cache*

Immediately after setting, you can see that a large number of system files under .venv and common libraries in the python virtual environment that were in the uncommitted state were grayed out and excluded from uncommitted treatment.

On the contrary, ** If you do not have the above two, a large number of files will be pushed **, so please be careful.

By this setting, .gitignore itself will not be committed. I don't see any existing public GitHub repositories containing gitignore, so it's best not to commit .gitignore.

7. Confirm PUSH operation to remote repository

Create a sample.py file in your project home for testing. Please note that flake8 will throw an error if you do not insert a line with only line breaks at the end. (** flake8, noisy ... **)

sample.py


print('Hello world')

If you click the source control icon on the left, the following screen will appear.

スクリーンショット 2021-01-03 23.59.59.png

The green U indicates the new file. Click + to put your changes in a staging state. Enter a message and press the checkmark (commit button) above it. If there are no staging files without errors, the commit to the local repository is complete.

In addition, select "Push" from the menu that appears with the ... button. For the first time, the following screen will appear in VS Code. If you press the permission and respond obediently to the Web screen and VS Code that appears, the cooperation will be completed.

スクリーンショット 2021-01-04 21.38.13.png

After the authentication expires after the first time, you will be asked for the keychain password at the same scene, so enter your Mac login password. If there are no errors, the push to the remote repository is completed successfully.

If you take a look at GitHub, you can see that the repository has been updated as shown below. スクリーンショット 2021-01-04 0.10.01.png

This is completed. If something goes wrong and you cannot recover, please delete everything under the project home and try again.

Recommended Posts

Install Python venv --VSCode --GitHub integration environment on Mac
Install Python environment on local PC (pyenv, venv on Mac)
Install Python on Mac
Install Python 3 on Mac
Install Python 3.4 on Mac
Install pygame on python3.4 on mac
Install pandas 0.14 on python3.4 [on Mac]
Install Python3 on Mac and build environment [Definitive Edition]
Building a Python environment on Mac
Install python2.7 on windows 32bit environment
Create a Python environment on Mac (2017/4)
Python environment construction memo on Mac
Install Python 3.7 Anaconda on MAC, but Python 2
Install python3 on Mac (El Capitan)
Environment construction of python3.8 on mac
Install Python development environment on Windows 10
Steps to install python3 on mac
VScode environment construction (on Mac) & graph display in Python (@browser)
Create a python environment on your Mac
Steps to install Python environment on Ubuntu
Python3 + venv + VSCode + macOS development environment construction
Build Python environment with Anaconda on Mac
python on mac
Install python package in personal environment on Ubuntu
Build a Python environment on Mac (Mountain Lion)
Build a Python development environment on your Mac
[Venv] Create a python virtual environment on Ubuntu
Install Tensorflow on Mac
Install python on WSL
Install pyenv on mac
Install Python on Pidora.
Install Scrapy on python3
Install Ansible on Mac
Install Python3.4 on CentOS 6.6
Install python on windows
Install Python 2.7.3 on CentOS 5.4
Install Python 3.3 on Ubuntu 12.04
Install Caffe on Mac
Install mecab on mac
Mac environment construction Python
Install mecab-python on Mac
Install Python 3.6 on Docker
Install selenium on Mac and try it with python
Steps to install the latest Python on your Mac
Build a machine learning Python environment on Mac OS
Build a Python development environment on Mac OS X
Build a Python environment on your Mac using pyenv
How to build a beautiful Python environment on a new Mac and install Jupter Notebook
Make it easy to install the ROS2 development environment with pip install on Python venv
Install Python 3.8 on RHEL 8 (AppStream)
Install watchdog on Windows + Python 3.3
Install Python environment with Anaconda
Pyramid + mongodb environment on Mac
Install Python 3.8 on CentOS 7 (SCL)
Install module on Anaconda (Mac)
Install OpenPose on mac (Catalina)
Install numba on your Mac
Python environment construction For Mac
Build Python environment on Windows
Handling of python on mac
Update python on Mac to 3.7-> 3.8