# Python:Visual Studio Code 在 WSL 中進行開發 ## 〔Step 1〕先安裝 WSL Windows Subsystem for Linux(WSL)的安裝方法可以參考我寫的這篇文章: [WSL:如何在 Win10 系統上安裝 Linux 子系統](https://withhhsong.com/wsl_linux/) ## 〔Step 2〕再安裝 Visual Studio Code VS Code 下載網址:https://code.visualstudio.com ### VS Code 跟 Visual Studio 差別在哪 Visual Studio Code(VS Code)跟 Visual Studio 是不同的,Visual Studio Code 是由微軟開發且跨平台的一款輕量級免費原始碼編輯器。VS Code 可以在 Linux 系統上運行,但 Visual Studio 無法。下圖左邊紫色的 icon 是 Visual Studio,右邊藍色的 icon 才是 VS Code。  ## 〔Step 3〕安裝 VS Code 擴充工具 Remote - WSL 裝好 VS Code 之後打開可以切到擴充工具的面板(Extensions)搜尋框上輸入「Remote - WSL」。Remote - WSL 允許我們在 Windows 系統上使用 VS Code 構建在 Windows Subsystem for Linux(WSL)上運行的 Linux 系統。也就是說 Remote - WSL 這個擴充工具可以讓我們在 WSL 中使用 VS Code,跟在 Windows 中完全一樣。(在擴充工具這裡也可以搜尋「Theme」來更換 VS Code 的介面主題外觀。)  如果在程式畫面左下角看到個圖案則代表 Remote - WSL 已經確認安裝成功。  ## 〔Step 4〕Python 開發(Development) 如果還沒有安裝過 Python,先打開 Linux 終端機(我這邊是 Ubuntu 20.04 LTS),執行 pip install 的命令將 Python3 pip 安裝到 Linux 系統中。 ```=bash sudo apt update ``` ```=bash sudo apt install python3 python3-pip ```  安裝好確認一下當前 Python 的版本,執行以下命令: ```=bash python3 --version ```  ## 〔Step 5〕在 WSL 中啟動 VS Code 我先在 C 槽新增一個叫 WSL 的資料夾,稍後要建立的檔案都會放在裡面。 * mkdir [+foldername] 可以在當前路徑建立新資料夾 * cd [+foldername] 可以進入該資料夾 ```=bash mkdir WSL && cd WSL ```  在 WSL 中啟動 VS Code 的方式是在 WSL 終端機中輸入```code .```   VS Code 會在 Linux 端安裝一個小型伺服器,然後 VS Code 將和它連接。這個伺服器將在 WSL 中安裝擴充,讓它能夠在 WSL 中安裝的工具和框架中正確地執行。意思是這些擴展是針對安裝在 WSL 中的框架執行的,而不是針對安裝在 Windows 端的框架。 指令執行完畢後,VS Code 會自動啟動,應該會跳出一則提示說明 VS Code 正在連接到 WSL,並且可以訪問 Node.js-based 的伺服器。  ## 〔Notes〕WSL home directory? VS Code 執行時可能會提示你: > This workspace is on the Windows file system (/mnt/). For best performance, we recommend moving the workspace to the Linux file system (~/home). linux 的 home 路徑會在這裡: ```=bash \\wsl$\Ubuntu-20.04\home\<username> ``` 如果開啟的時候是在 ```/mnt/c/WSL``` 路徑上,在開啟舊檔這邊輸入```~```就會導向 linux 目錄。  初始工作區位置會在```/mnt/c```,VS Code 會推薦你把路徑改到``` ~/home ``` 我們可以用指令:```cp -R```複製原本目錄資料夾下的所有檔案: ```=bash cp -R /mnt/c/WSL/ ~/my_project ``` 在 WSL 終端進入 linux 的 ```\home\<username>``` 路徑: ```=bash cd ~/my_project ```  新建一個 hello 的 python 檔案: ```=bash echo 'print("hello world")' >> hello.py ``` 再啟用 VS Code(那個 . 是引數不可省略,代表在當前路徑打開資料夾) ```=bash code . ```  ## 〔Command〕Linux:cp -parameter | 參數 | 功能 | | ---- | ---------------------------- | | -a | 除了具有-p參數功能外,還可以加入SElinux屬性 | | -i | 如果要複製過去的位置已經有相同檔案,覆蓋前會詢問 | | -p | 將檔案本身屬性(權限、所有者、時間)同時複製過去 | | -r | 複製目錄資料夾下的所有檔案 | | -s | 複製成捷徑連結檔案 ## 〔Reference〕 https://code.visualstudio.com/docs/remote/wsl-tutorial https://solidstudio.io/blog/windows-subsystem-for-linux-explained https://superuser.com/questions/1185033/what-is-the-home-directory-on-windows-subsystem-for- https://stackoverflow.com/questions/66753829/how-to-move-workspacea-simple-project-from-windows-file-system-mnt-to-linu https://dylan237.github.io/linux-basic.html --- 衍生閱讀: [WSL:如何在 Win10 系統上安裝 Linux 子系統](https://withhhsong.com/wsl_linux/) 衍生閱讀: [WslRegisterDistribution Error 0x80370102 解決方法](https://withhhsong.com/wsl_error_0x80370102) --- : : 20211007 : : 與松 withhhsong : : ###### tags: `withhhsong` `tutorials` `python` `wsl`
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.