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.
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.
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.
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.
Due to the 2020 BLM movement, the default branch name on GitHub has changed from master to main.
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.
Hereafter, the area directly under the local repository name directory is called "project home".
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.
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.
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.
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.
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.
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.
This is completed. If something goes wrong and you cannot recover, please delete everything under the project home and try again.
Recommended Posts