How to pass VScode + WSL + Python Path (Unable to import ~ "xxx" solution)

Background of writing the article

This is a summary of what the posters who are new to programming have investigated about the points that were stuck in environmental development. It may contain mistakes. In that case, I would appreciate it if you could let me know.

Subject of this article

I created a Python environment with WSL, but when I try to run it with VScode, I get an error "Unable to import ~" and it's annoying!

Execution environment

Windows10 WSL executable

Cause

It's because Vscode doesn't tell you exactly where Pytho3 is on WSL.

Solution

    1. Enter python3 in WSL. Run python3 in interactive mode
C:\programming>python
Python 3.8.2 (default, Jul 16 2020, 14:00:26)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
  1. Then python3 will be in interactive mode, so enter import sys. Find where python is as sys.path.
>>> import sys
>>> sys.path
['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/(User name)/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']

In this case, the second'/usr/lib/python3.8'is the Path value.

    1. Move to the folder with the ".py" file in VScode. If not, make a new one.

Four. Open ".vscode" in that folder. Open settings.json and copy the previous Path to pythonPath.

{
    "python.pythonPath": "/usr/lib/python3.8"
}

Five. Complete

reference

https://qiita.com/dynamonda/items/5a8129cd6e9cc139d94a https://qiita.com/ktgwaaa/items/6d1f54d5ff3c4559f96c

Afterword

For some reason, it seems that you have to use Remote WSL to fix this called Select Python Interpreter.

Recommended Posts

How to pass VScode + WSL + Python Path (Unable to import ~ "xxx" solution)
[Python] How to import the library
[Python] How to use import sys sys.argv
[Python / Tkinter] How to pass arguments to command
"How to pass PATH" to learn with homebrew
Summary of how to import files in Python 3
Let's understand how to pass arguments (Python version)
How to add a Python module search path
How to install Python
[Reintroduction to python] How to import via the parent directory
How to install python
unable to import django
What to do when "cannot import name xxx" [Python]
Execute Python function from Powershell (how to pass arguments)
How to use import
How to import Python library set up in EFS to Lambda
A story about how to specify a relative path in python.
How to import a file anywhere you like in Python
Library path setting to pass GAE / Python local unit tests
(Note) How to pass the path of your own module
[2020.8 latest] How to install Python
How Python module import works
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
[Python] Another way to import
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[VSCode] unable to import'google.cloud' What to do when pylint (import-error) does not recognize the Python import statement
How to pass arguments to a Python script in SPSS Modeler Batch
How to import CSV and TSV files into SQLite with Python
[Introduction to Udemy Python3 + Application] 69. Import of absolute path and relative path
[Beginner memo] How to specify the library reading path in Python
How to install python using anaconda
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with
Easy way to customize Python import
[Python] How to derive nCk (ABC156-D)
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
How to get the Python version
How to get started with Python
[Python] How to use list 3 Added
Unable to install Python with pyenv
How to use Mysql in python