Try it when Visual Studio Code can't load a Python module

Solution

Select Code → Preferences → Settings. Alternatively, you can use the shortcut key "command +,". スクリーンショット 2020-11-13 17.54.22.png

Click the small icon in the upper right to open it. スクリーンショット 2020-11-13 17.56.09.png

A file called settings.json will open. You can edit the destination of the Visual Studio Code path, so set "python.pythonPath ~" here. The path itself depends on your environment, so please set it. If there is a python path here from the beginning, you can overwrite it, and if you do not have it, you can write a new one. Just in case, please be able to restore it. スクリーンショット 2020-11-13 17.59.00.png

bonus

By the way, I will show you how to check which path python is reading. Seeing is believing, so please take a look at the screenshots first. If you get a quick glance and get "I see", please try it immediately. For those who are a little scared, I will give a brief explanation. スクリーンショット 2020-11-13 18.20.50.png

First, start python in your home directory. (To distinguish it from the python2 series originally on the Mac, I started it with the command python3. Please use the command that suits your environment as well)

When python starts, ">>>" will be displayed, so you can interact with python. Type "import sys" and press Enter. (Nothing happens, but that's okay.) Type "import pprint" and press Enter. (Nothing happens, but that's okay.) Type pprint.pprint (sys.path) and press Enter. Here python will tell you the path. Since it is loaded in order from the top, copy the path where the module is located and paste it into settings.json of vscode. You can end the interaction with python with "control + D".

If you do not have the expected path, you will need to pass it through, so please check it separately. In addition, I will omit the details of each command.

Personally, the point is to use pprint. However, when you hear the pass, it is written as sloppy on the side and spit out, so it is very difficult to read ... It is a device to solve it.

Background

Suddenly one day, the module I was using was getting an import error. I forgot the specific error content, but I think it was like "There is no module with that name". I thought that it shouldn't have been possible even though I could use it until then, but suddenly I remembered that I put anyenv and guessed that the destination of the path was changed. First of all, I was messing around with local environment variables (reading destinations of python2 and 3 paths, priority, storage location of modules installed with pip, etc.), but in the end I was messing around with the above. I solved it by the method of. I never thought I would adjust the editor side ... I still haven't trained enough. By the way, I also experienced a crisis situation where no command responded at the terminal while playing with various paths, but this was resolved fairly quickly. (Thanks to all the informants on the internet!)

However, I still have some doubts ... The reason is that only one of the several imports (specifically jaconv) suddenly became unusable. Why did I get such an error even though I could use another module with the same file? And I'm wondering if this solution is the best ... If the cause is found, I will add it.

Recommended Posts

Try it when Visual Studio Code can't load a Python module
Try to create a python environment with Visual Studio Code & WSL
A note I was addicted to when running Python with Visual Studio Code
Set up a Python development environment with Visual Studio Code
Do something like a Python interpreter in Visual Studio Code
Installation of Visual studio code and installation of python
I get an error when I put a Python plugin in Visual Studio Code under the pyenv environment
Error when installing a module with Python pip
Build Python development environment with Visual Studio Code
When I try matplotlib in Python, it says'cairo.Context'
Time when terminal disappears in Visual Studio Code
Settings for Python coding in Visual Studio Code
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
Create a Python development environment in 10 minutes (Mac OS X + Visual Studio Code)
I customized it with Visual Studio Code (mainly for python), so I will summarize it
Try to make a Python module in C language
Create a Python module
Try debugging Python on Raspberry Pi with Visual Studio.
A memorandum when writing experimental code ~ Logging in python
Make Visual Studio Code autocomplete for python external libraries
Python development environment with Windows + Anaconda3 + Visual Studio Code
Python development environment with Windows + Python + PipEnv + Visual Studio Code
Japanese output when dealing with python in visual studio
A memo of misunderstanding when trying to load the entire self-made module with Python3
Enable the virtualenv Python virtual environment for Visual Studio Code
Install python and Visual Studio Code on windows10 (April 2020 version)
Python development in Visual Studio
[Python, Selenium, PhantomJS] A story when scraping a website with lazy load
Bash, Python, Javascript, code command, etc. in Visual Studio Code on Mac
I get a can't set attribute when using @property in python
When a Python beginner tried using Bottle, it worked unexpectedly easily.
A memo for those who use Python in Visual Studio (me)
Java with Visual Studio Code (Part 2)
Visual Studio Code may be good
[Code] Module and Python version output
Precautions when creating a Python generator
Try using the Python Cmd module
[VS Code] ~ Tips when using python ~
When writing a program in Python
Use Jupyter Notebook with Visual Studio Code on Windows 10 + Python + Poetry + pyenv-win
From re-environment construction of Python to graph drawing (on visual studio code)
When I try to go back using chainer, it fits a little