{"id":20619685,"url":"https://github.com/twtrubiks/python-creation-of-virtual-environments","last_synced_at":"2025-06-29T05:35:47.882Z","repository":{"id":84518984,"uuid":"150916115","full_name":"twtrubiks/python-creation-of-virtual-environments","owner":"twtrubiks","description":"如何使用 venv 建立 virtual environments 📝","archived":false,"fork":false,"pushed_at":"2024-04-20T03:59:18.000Z","size":5,"stargazers_count":20,"open_issues_count":0,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T11:55:09.148Z","etag":null,"topics":["python","tutorial","venv","virtual-environments"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twtrubiks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-30T01:08:09.000Z","updated_at":"2025-01-07T13:39:09.000Z","dependencies_parsed_at":"2024-11-16T12:22:21.305Z","dependency_job_id":null,"html_url":"https://github.com/twtrubiks/python-creation-of-virtual-environments","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/twtrubiks/python-creation-of-virtual-environments","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fpython-creation-of-virtual-environments","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fpython-creation-of-virtual-environments/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fpython-creation-of-virtual-environments/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fpython-creation-of-virtual-environments/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twtrubiks","download_url":"https://codeload.github.com/twtrubiks/python-creation-of-virtual-environments/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fpython-creation-of-virtual-environments/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262543974,"owners_count":23326632,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","tutorial","venv","virtual-environments"],"created_at":"2024-11-16T12:12:16.434Z","updated_at":"2025-06-29T05:35:47.817Z","avatar_url":"https://github.com/twtrubiks.png","language":null,"readme":"# python-creation-of-virtual-environments\n\n如何使用 venv 建立 virtual environments  📝\n\n* [Youtube Tutorial - How to use venv to create virtual environments - python](https://youtu.be/LzaBf2QjR8s)\n\n* 更進階的方式(可多版本) - [pyenv 教學](https://github.com/twtrubiks/python-notes/tree/master/pyenv_tutorial)\n\n## 簡介\n\n在 python 中，我們常常需要建立環境應付各個 project，如果沒有切割環境，會非常難維護，\n\n所以，建立 virtual environments 是一件非常重要的事情:satisfied:\n\n以前我也有介紹過如何建立 virtual environments，那時候是使用 Anaconda，文章可參考 [這裡](https://github.com/twtrubiks/FaceDetect/tree/master/How%20Easy%20Install%20OpenCV%20%20for%20Python%20use%20Anaconda)，\n\n不過最近重灌電腦，就覺得裝 Anaconda 超級花時間( 因為 Anaconda 檔案略大:persevere: )，\n\n而且正式機上，通常也不太會使用 Anaconda 建立環境，所以，今天就和大家介紹官方文件建議的做法，\n\n使用 [venv](https://docs.python.org/3/library/venv.html) 建立 virtual environments 吧:grinning:\n\n## Linux 教學\n\n假如我今天想要安裝 python3.8, 直接執行以下指令即可(依照自己需要的 python 版本做修改)\n\n```cmd\nsudo apt install python3.8 python3.8-venv python3-venv\n```\n\n建立 venv\n\n```cmd\npython3.8 -m venv my-py-38-venv\n```\n\n啟動 venv 環境\n\n```cmd\nsource my-py-38-venv/bin/activate\n```\n\n## Windows 教學\n\n### 安裝 python\n首先，一定要先下載 [python](https://www.python.org/downloads/)，當你在安裝時，他會問你是否要加入環境變數，請記得要把他打勾:thumbsup:\n\n預設是沒有打勾的，安裝完畢後，使用 terminal 輸入 python ( 或 python3 ) 確認是否可以找到 ( 如下圖 )，\n\n![alt tag](https://i.imgur.com/sL7tfoF.png)\n\n如果出現 `'python' 不是內部或外部命令、可執行的程式或批次檔。`，就代表你的環境變數沒設定。\n\n( 有兩種方法可以解決，第一種是移除 python 重新安裝，然後安裝時記得打勾加入環境變數， 第二種是自己加入環境變數 )\n\n，環境變數的部分如下，像是我自己有裝兩種不同版本的 python，\n\n![alt tag](https://i.imgur.com/RDhWhoO.png)\n\n### 如何使用 venv 建立 virtual environments\n\n首先，切到目錄 (看你想要統一 venv 在哪個資料夾)底下，接著執行以下指令，\n\n```cmd\npython -m venv tutorial-venv\n```\n\n![alt tag](https://i.imgur.com/hG1vSoh.png)\n\n如上圖，當右邊的指令執行完之後，目標資料夾底下就會多出一個 tutorial-venv ( 你所指定的名稱 )。\n\n### 如何啟動 virtual environments\n\n要啟動 virtual environments 也很簡單，直接執行 activate.bat，他在 tutorial-venv/Scripts/activate.bat 底下，\n\n```cmd\ntutorial-venv\\Scripts\\activate.bat\n```\n\n補充 Linux or Mac\n\n```cmd\nsource activate\n```\n\n如下圖，執行在 terminal 中輸入 `activate.bat` 即可，\n\n![alt tag](https://i.imgur.com/zTrvEP8.png)\n\n如上圖，這樣就成功啟動 virtual environments 了:satisfied:\n\n如果想要關閉或退出，執行 deactivate.bat 即可，如下圖，\n\n![alt tag](https://i.imgur.com/R8Y9fma.png)\n\n在該環境下安裝的任何 library 都不會去影響到其他的 virtual environments，\n\n舉個例子，如下圖，我在 tutorial-venv 環境中安裝了 requests，所安裝的 library，\n\n是不會去影響到其他的 virtual environments，\n\n( 下圖左邊為本機的 python，下圖右邊為 virtual environments )\n\n![alt tag](https://i.imgur.com/NicARVa.png)\n\n可以看到，在 virtual environments 中安裝的 requests 是不會影響到本機中的 python 環境:thumbsup:\n\n最後，依照自己使用的 ide 把 venv 環境指定好就可以了:relaxed:\n\n## 執行環境\n\n* windows 10\n\n## Reference\n\n* [Creation of virtual environments](https://docs.python.org/3/library/venv.html)\n\n## Donation\n\n文章都是我自己研究內化後原創，如果有幫助到您，也想鼓勵我的話，歡迎請我喝一杯咖啡:laughing:\n\n![alt tag](https://i.imgur.com/LRct9xa.png)\n\n[贊助者付款](https://payment.opay.tw/Broadcaster/Donate/9E47FDEF85ABE383A0F5FC6A218606F8)\n\n## License\n\nMIT license\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fpython-creation-of-virtual-environments","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwtrubiks%2Fpython-creation-of-virtual-environments","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fpython-creation-of-virtual-environments/lists"}