I want to manage the dependency of Python execution environment for each project using VSCode.
Clone pyenv to .pyenv in your home directory. You can use homebrew, but if you want to switch between multiple Python versions, you should clone from github.
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Describe the following in .bash_profile and .zshrc.
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
source ~/.bash_profile
After rebooting, the pyenv
command is available.
Show the installable Python.
pyenv install --list
Install Python by specifying the version. (Install 3.8.0 this time)
pyenv install 3.8.0
Specify the Python to use as follows. (Use pyenv local if you want to specify it locally.)
pyenv global 3.8.0
Make sure that the version reflects 3.8.0.
python --version
Create an independent project for each Python so that it is not affected by other projects. It looks like a feature built under Python 3.x.x.
Create and move the project directory. (It may be directly under your home directory.)
mkdir py_project_1 && cd $_
Do the following in the py_project_1 directory: Create it with the name venv directly under the project directory.
python3 -m venv venv
(It doesn't have to be venv, it can be envdir. In that case, run `` `python3 -m venv envdir```. For the sake of clarity, venv seems to be good.)
Run and start source.
source venv/bin/activate
When leaving the virtual environment
deactivate
Create a virtual environment for each python environment project, divide the dependency version, etc., and create an environment that does not affect other projects. (For example, the library installed in project A cannot be used in project B, it is not displayed in pip list, and even if the same library is used, it can be handled even if the version is different.)
Change to the project directory.
cd py_project_1
Add the py_project_1 directory to your vscode workspace.
vscode menu>File>Add folder to workspace
Enter the virtual environment.
source venv/bin/activate
Create settings.json for use with vscode.
mkdir vscode && cd $_
touch settings.json
Check the python path of the virtual environment.
which python
Describe the following in settings.json. Describe the path confirmed by which python in python.pythonPath.
settings.json
{
"python.pythonPath": "Describe the path confirmed by which python",
"python.venvFolders": [
"venv"
]
}
Move to the py_project_1 directory, enter the virtual environment, and execute the following command.
pip install pytest
After installing pytest, confirm that it was installed.
pip list
Leave the virtual environment and check the pip installation status (pip list is OK). If pytest is not reflected, you can confirm that you have created an environment that does not affect other projects.
Recommended Posts