Build a development environment like the title and make it easier to migrate.
brew install python3
is the easiest?)
--Installed VS Code code
command--Develop only with Python3
--I want to use VS Code
--I want you to do IntelliSense, Linting, Formatting with VS Code
--I want to save the list of packages installed with pip
and rebuild it in another environment.
--I don't want to pollute the global environment with global pip
I made a tool that meets the requirements. https://gist.github.com/KoheiKanagu/b3ba2bb68d36509b6cee054e077bd415
$ python3 pset.py -h
usage: pset.py [-h] [--init] [-i] [-s] [-j] [-c]
This script deitals : http://qiita.com/KoheiKanagu/items/752c64aeeb154970d22e
optional arguments:
-h, --help show this help message and exit
--init Initialize Python virtual environment
-i, --install Install packages from "requirements.txt"
-s, --sync Sync packages using of pip-compile and pip-sync
-c, --code Open VSCode at virtual environment
--Use python3 -m venv
to create a virtual environment named **. env **.
--Install pip-tools
-If there is no ** requirements.in **, create it and write pip-tools.
--install If there is ** requirements.txt ** in the same directory, install the packages with pip install -r requirements.txt
.
-Pip-compile ** requirements.in ** and pip-sync
--source .env / bin / activate && code ./
to start VS Code that inherited the virtual environment.
Formatting All you have to do is install ** autopep8 ** or something in your global environment.
Like Linting, which will be described later, formatter does not need to look at packages in the virtual environment, so even if it is installed globally, it will format properly.
Linting For example, if you use ** pylint **, even if you install it globally, IntelliSense will not be possible because it will not see the packages installed in the virtual environment.
So, if you install ** pylint ** in the virtual environment, it will look at the packages in the virtual environment, so you can use IntelliSense.
Similarly, jupyter can only find packages if it is installed in a virtual environment.
With the tool,
Start VS Code like source .env / bin / activate && code ./
.
Therefore, as you can see by having VSCode do pip freeze
etc. on the terminal, VSCode has taken over the virtual environment.
Therefore, when the program is executed and executed from the terminal, it is executed in the virtual environment.
You can also run it in a virtual environment using Code Runner.
Since **. env ** appears in the explorer on the left side of the VS Code screen, the contents will be caught in the search, and Linter will be squeezed for various files such as html and css inside, so as follows Add to files.exclude
.
Then it will be ignored and will not be displayed in Explorer, and Linter will not get angry.
Of course there is no problem in the virtual environment
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/.env/**": true
}
Do source .env / bin / activate
and then pip install
.
However, since we are assuming the use of pip-compile and pip-sync, it is easier to write the necessary packages in ** requirements.in ** and do python3 pset.py --sync
.
Note that if you --sync with nothing written in ** requirements.in **, all installed packages will be deleted by the pip-sync function.
tips
I think it can be done by linking with ** pyenv ** etc., but I personally don't need 2.7, so I haven't tried it.
When you create a virtual environment in such a ~ / MyProject / .env / *
directory name, if you change the name to ~ / MyProject2 / .env / *
, it will be source ~ / MyProject2 /. Even if you do env / bin / activate
, you cannot move to the virtual environment.
In that case, delete .env
and reset it.
Recommended Posts