[Memorandum] python + vscode + pipenv It's common, but it was a mess with warning, so a memorandum

This is the first post. Until now, I used notepad ++ to write code, but I decided to use VScode because it was convenient. It's for studying python, but I created an environment because I wanted to make it easier to manage by using pipenv. However, the warning did not disappear and I had a lot of trouble, so I will leave it here as a memorandum.

The environment to be built this time is as follows. -OS:windows10 -Python:3.8 -Library used: pipenv, Django

1. Install VScode

Please install the one that suits your environment from the following page. https://code.visualstudio.com/download

2. Install python

Please download and install the version you want according to the environment from the following page. https://www.python.org/downloads/windows/ Also, in the case of windows, there should be a checkbox to add an environment variable in the middle, so don't forget to check it. (There is a checkbox for words like "Add pythonX.X to PATH".) After installing, let's confirm that the installation is completed with the following command.

python --version

If the version information is displayed, it means that the installation was successful. (If the PATH does not pass, the command will not be recognized here.)

3. Install Pipenv.

Let's install it from 2 as it is. Execute the following command.

pip install pipenv

After installing pipenv, add environment variables. Add the following environment variable and set the value to "true".

PIPENV_VENV_IN_PROJECT

By doing this, when you create a virtual environment with pipenv, a ".venv" folder will be created in the working directory. If you do not add this environment variable, it will be created under the user folder. There is still no problem, but it is recommended to set it because it is troublesome when managing the environment and folders on VScode later.

4. Set VScode.

  1. Search for and install the following from Extensions in the sidebar. (A useful tool package for Python. It does Lint function and automatic indentation.)
python extension for visual studio code
  1. Open the settings in "File"-> "Basic Settings" and set the following in "Python: Venv Path" in "Extensions"-> "python".
.venv,

If you set this, it seems that when you open the workspace, it will load the .venv folder directly under it. 3. Restart VScode once. Load the settings up to this point. 4. We will create the environment in earnest from here. Open the folder you work in from "File"-> "Open Folder". 5. Select "Terminal"-> "New Terminal" to open the terminal. 6. Create a virtual environment with pipenv. Execute the following command in the terminal.

pipenv install

At this time, you will be asked in English something like "It looks like a virtual environment was created, but do you want to use it?", So select "YES". Then, I think that the folder structure will be as follows.

.
├─.venv ← Made with pipenv
├─.vscode ← Created by clicking Yes
├─Pipfile ← Made with pipenv
└─Pipfile.lock ← Made with pipenv

If .vscode was not created (or if you click No), create a .py file with an appropriate name directly under the workspace. Then it should be made. 7. Create a folder named ".env" directly under the workspace and write the following.

PYTHONPATH=.venv/src
  1. Add the following to "./.vscode/setting.json".
"python.envFile": "${workspaceFolder}\\.env"

Don't forget to double the backslash as it will be recognized as an escape key. By following steps 7 and 8, you will not be able to load the library in the virtual environment and get a warning.

later

pipienv install xxx

Install your favorite library with

pipenv shell

You can develop in your favorite virtual environment by executing! that's all! Thank you for your hard work!

Finally

Until now, I didn't really care about it because I just wanted to type the code, but depending on the site, vscode is standard and it is convenient to link with git, so I started using it. From the point of writing this article, the eraser in my head is still working hard today I created it while struggling with the disappearing memories.

Do you know this too? Not here

If you have any questions, please let us know. Thank you to all of our ancestors.

Recommended Posts

[Memorandum] python + vscode + pipenv It's common, but it was a mess with warning, so a memorandum
It's Halloween so I'll try to hide it with Python
Homework was a pain, so I do management accounting with Python
I made a segment tree with python, so I will introduce it
When I made a treemap (area graph) with python, it was subtle, so when I used flourish, it felt pretty good.
When I connect to a remote Jupyter Server with VScode, it's remote but local
I was able to mock AWS-Batch with python, moto, so I will leave it
A memorandum of calling Python from Common Lisp
I couldn't import the python module with VSCODE, but I could do it on juoyterlab, so I'm talking about finding the cause.
I wondered if Python 3.4 was faster, but it was slower
conda memorandum: Building a Python environment with supercomputer ITO
I wrote python3.4 in .envrc with direnv and allowed it, but I got a syntax error
It's a hassle to write "coding: utf-8" in Python, so I'll do something with Shellscript
There was a doppelganger, so I tried to distinguish it with artificial intelligence (laughs) (Part 1)