mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
Merge pull request #27905 from Cubik65536/translate-tech-20221109-6-Using-Python-in-VS-Code-and-Codium
[翻译完成][tech]: 20221109.6 ⭐️ Using Python in VS Code and Codium.md
This commit is contained in:
commit
bd07744b61
@ -1,113 +0,0 @@
|
||||
[#]: subject: "Using Python in VS Code and Codium"
|
||||
[#]: via: "https://opensource.com/article/22/11/python-vs-code-codium"
|
||||
[#]: author: "Don Watkins https://opensource.com/users/don-watkins"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "Cubik65536"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Using Python in VS Code and Codium
|
||||
======
|
||||
|
||||
If you're looking for a good, general-purpose, open source code editor with Python integration, then you might give Codium a try.
|
||||
|
||||
Over the past couple of years, I have had the privilege of working with middle school children to introduce them to [Python coding][1] and the Raspberry Pi 400. It's been a lot of fun, and the Pi has been a great platform for the students and me. We've used [Code with Mu][2] and it's been quite successful. Our aptitude with Python has grown with experience, and so recently I started looking for ways to offer these students more.
|
||||
|
||||
I participated in a Python learning class, and during that class I got introduced to Microsoft's Visual Studio Code. I learned a lot in that class about how to set up a virtual environment for Python, and how to configure VS Code for Python programming. During that learning journey, I also got introduced to [Codium][3], which is essentially VS Code without Microsoft's branding and telemetry.
|
||||
|
||||
If you're looking for a good, general-purpose, open source code editor with Python integration, then you might give Codium a try. Here's how I got Codium set up for Python on my Linux system.
|
||||
|
||||
### Install or update Python on Linux
|
||||
|
||||
First, make sure you are running the latest version of Python. You can do this with your package manager. On Debian and Debian-based systems:
|
||||
|
||||
```
|
||||
$ sudo apt install python3-pip
|
||||
```
|
||||
|
||||
On Fedora, CentOS, Mageia, OpenMandriva, and similar:
|
||||
|
||||
```
|
||||
$ sudo dnf update python3
|
||||
```
|
||||
|
||||
On some systems, you may also need to install the software to create Python virtual environments:
|
||||
|
||||
```
|
||||
$ sudo apt install python3.10-venv
|
||||
```
|
||||
|
||||
### Install Codium
|
||||
|
||||
Next, [install Codium][4] on your computer. On Linux, you can download a package and install it with your package manager, or [use the Flatpak][5].
|
||||
|
||||
To launch Codium once it's installed, open your application or Activities menu and type "Code".
|
||||
|
||||
### Install the VS Code Python extension
|
||||
|
||||
There's nothing special about code. It's just plain text that gets interpreted by some other application, whether it's a compiler or a runtime. You can write Python code in Codium without special extensions. However, having a Python extension adds several conveniences.
|
||||
|
||||
Click on the **File** menu, select **Preferences**, and choose **Extensions**. In the **Extensions** panel, find the Python IntelliSense extension.
|
||||
|
||||
![VS Code and Codium have an extension manager that opens in the left column, allowing you to install add-on modules.][6]
|
||||
|
||||
You've got Python set up in Codium. All that's left to do is to put it to good use.
|
||||
|
||||
### Setup a virtual environment for VS Code or Codium
|
||||
|
||||
You can create a project directory and add it to Codium so that while you work, the files you create and save default to the active project directory. It's a fast way to stay organized, and it saves you from having to click around File Save and Open dialogues constantly.
|
||||
|
||||
When you create a virtual Python environment as a work folder, Codium (because you have the Python extension installed) detects it. When you activate a virtual environment folder as the active project directory, Codium automatically runs the activation code required to use the virtual environment.
|
||||
|
||||
To create a virtual environment for Python, open a terminal and type:
|
||||
|
||||
```
|
||||
$ python3 -m venv ~/PythonCoding
|
||||
```
|
||||
|
||||
### Add a project directory
|
||||
|
||||
In Codium, click on the **File** menu and choose **Add Folder to Workspace**. Open the virtual environment you've just set up (for me, that's `/home/don/PythonCoding`.)
|
||||
|
||||
Now you're ready to write some Python code! Create a new Python file in your workspace and insert some basic code. You may notice, as you type, that Codium helpfully suggests auto-completion for the Python modules the environment contains.
|
||||
|
||||
```
|
||||
importsysprint("Codium running Python " + sys.version)
|
||||
```
|
||||
|
||||
Now click the **Play** button in the top right corner of the Codium window. This opens a console panel at the bottom of the window, displaying the output of your code:
|
||||
|
||||
```
|
||||
(PythonCode) sh-5.1$ /home/bogus/PythonCode/bin/python /home/bogus/PythonCode/app.py
|
||||
Codium running Python 3.10.6 (main…)[GCC 12.1.0](PythonCode) sh-5.1$
|
||||
```
|
||||
|
||||
As you can see from this output, Codium is running within the `PythonCode` environment, and it's successfully run your Python code.
|
||||
|
||||
### Codium and Python
|
||||
|
||||
Using Codium for Python makes writing and running code easier than ever, but Python isn't the only language Codium supports. You can easily find and install other extensions from the [Open VSX Registry][7], a vendor-neutral open source "marketplace" for VS Code extensions.
|
||||
|
||||
The Codium interface is more complicated than some basic editors, but it has what I'm looking for at this point in my learning journey. If you're looking to graduate to something professional, or you're looking to switch from your current editor to something new, then give Codium a try.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/22/11/python-vs-code-codium
|
||||
|
||||
作者:[Don Watkins][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[Cubik65536](https://github.com/Cubik65536)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/don-watkins
|
||||
[b]: https://github.com/lkxed
|
||||
[1]: https://opensource.com/article/22/8/math-python-raspberry-pi
|
||||
[2]: https://codewith.mu/
|
||||
[3]: https://opensource.com/article/20/6/open-source-alternatives-vs-code
|
||||
[4]: https://github.com/VSCodium/vscodium/releases
|
||||
[5]: https://flathub.org/apps/details/com.vscodium.codium
|
||||
[6]: https://opensource.com/sites/default/files/2022-10/codium-extension-python.webp
|
||||
[7]: https://open-vsx.org/
|
@ -0,0 +1,117 @@
|
||||
[#]: subject: "Using Python in VS Code and Codium"
|
||||
[#]: via: "https://opensource.com/article/22/11/python-vs-code-codium"
|
||||
[#]: author: "Don Watkins https://opensource.com/users/don-watkins"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "Cubik65536"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
在 VS Code 和 Codium 中使用 Python
|
||||
======
|
||||
|
||||
如果你正在寻找一个优秀的、通用的、开源的、带有 Python 集成的代码编辑器,那么你可以尝试一下 Codium。
|
||||
|
||||
在过去几年内,我有幸和中学生们一起,并带他们入门 [Python 开发][1] 和 Raspberry Pi 400。这一切都很有趣,Pi 对于学生和我来说都是一个很好的平台。我们使用了 [Code with Mu][2],并且一切都很成功。我们的 Python 技能随着经验的增长而增长,因此最近我开始寻找给这些学生提供更多东西的方法。
|
||||
|
||||
我参与了一个 Python 课程并在课程中接触了 Microsoft 的 Visual Studio Code。我在课程中学到了很多关于如何为 Python 设置虚拟环境以及如何为 Python 编程配置 VS Code 的知识。在学习过程中,我也认识了 [Codium][3],它本质上是 VS Code,但没有 Microsoft 的品牌和遥测。
|
||||
|
||||
如果你正在寻找一个优秀的、通用的、开源的、带有 Python 集成的代码编辑器,那么你可以尝试一下 Codium。下面是我在 Linux 系统上为 Python 设置 Codium 的方法。
|
||||
|
||||
### 在 Linux 上安装或更新 Python
|
||||
|
||||
首先,确保你正在运行最新版本的 Python。你可以使用你的软件包管理器来完成这项工作。在 Debian 和基于 Debian 的系统上:
|
||||
|
||||
```
|
||||
$ sudo apt install python3-pip
|
||||
```
|
||||
|
||||
在 Fedora、CentOS、Mageia、OpenMandriva 和类似的系统上:
|
||||
|
||||
```
|
||||
$ sudo dnf update python3
|
||||
```
|
||||
|
||||
在某些系统上,你可能还需要安装创建 Python 虚拟环境的软件:
|
||||
|
||||
```
|
||||
$ sudo apt install python3.10-venv
|
||||
```
|
||||
|
||||
### 安装 Codium
|
||||
|
||||
接下来,在你的电脑上 [安装 Codium][4]。在 Linux 上,你可以下载一个包并使用你的包管理器安装它,或者 [使用 Flatpak][5]。
|
||||
|
||||
在安装好 Codium 之后,打开你的应用程序或活动菜单,输入 “Code” 以启动它。
|
||||
|
||||
### 安装 VS Code Python 扩展
|
||||
|
||||
代码其实不是什么特别的东西。它只是一些其他应用程序(编译器或运行时)解释的纯文本。你可以在 Codium 中编写 Python 代码而不需要特殊的扩展。但是,有一个 Python 扩展可以为你带来一些方便的功能。
|
||||
|
||||
点击**文件**菜单,选择**首选项**,然后选择**扩展**。在**扩展**面板中,搜索 Python IntelliSense 扩展。
|
||||
|
||||
![VS Code 和 Codium 都有一个扩展管理器,它会在页面左侧打开,允许你安装附加模块。][6]
|
||||
|
||||
你已经在 Codium 中设置了 Python。剩下的就是把它用起来。
|
||||
|
||||
### 为 VS Code 或 Codium 设置虚拟环境
|
||||
|
||||
我们可以创建一个项目目录并将其添加到 Codium 中,这样在工作时,你创建和保存的文件都将默认保存到活动项目目录。这是一种快速的管理方式,可以让你不必经常点击文件保存和打开对话框。
|
||||
|
||||
在你创建一个虚拟 Python 环境作为工作目录时,Codium(因为你已经安装了 Python 扩展)会检测到它。当你激活一个虚拟环境文件夹作为活动项目目录时,Codium 会自动运行使用虚拟环境所需的激活代码。
|
||||
|
||||
要为 Python 创建一个虚拟环境,请打开终端并输入:
|
||||
|
||||
```
|
||||
$ python3 -m venv ~/PythonCoding
|
||||
```
|
||||
|
||||
### 添加项目目录
|
||||
|
||||
在 Codium 中,点击**文件**菜单,选择**将文件夹添加到工作区**。打开你刚刚设置的虚拟环境(对我来说,是 `/home/don/PythonCoding`)。
|
||||
|
||||
现在你已经准备好写一些 Python 代码了!在你的工作区中创建一个新的 Python 文件并插入一些基本代码。当你输入时,你可能会注意到,Codium 会为环境包含的 Python 模块提供自动补齐建议。
|
||||
|
||||
``` python
|
||||
import sys
|
||||
print("Codium running Python " + sys.version)
|
||||
```
|
||||
|
||||
现在点击 Codium 窗口右上角的**运行**按钮。这会在窗口底部打开一个控制台面板显示你的代码的输出:
|
||||
|
||||
```
|
||||
(PythonCode) sh-5.1$ /home/bogus/PythonCode/bin/python
|
||||
/home/bogus/PythonCode/app.py
|
||||
Codium running Python 3.10.6 (main…)[GCC 12.1.0]
|
||||
(PythonCode) sh-5.1$
|
||||
```
|
||||
|
||||
就像你从输出中看到的,Codium 在 `PythonCode` 环境中运行,并成功运行了你的 Python 代码。
|
||||
|
||||
### Codium 和 Python
|
||||
|
||||
使用 Codium 编写 Python 代码比以往任何时候都更容易,但 Python 并不是 Codium 支持的唯一语言。你可以轻松地从 [Open VSX Registry][7] 中找到并安装其他扩展,这是一个中立的开源 VS Code 扩展 “市场”。
|
||||
|
||||
Codium 的界面比一些基本的编辑器更复杂,但它有我在学习过程中所需要的东西。如果你需要一个更专业的编辑器,或者你想从当前的编辑器切换到新的编辑器,那么试试 Codium 吧。
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/22/11/python-vs-code-codium
|
||||
|
||||
作者:[Don Watkins][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[Cubik65536](https://github.com/Cubik65536)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/don-watkins
|
||||
[b]: https://github.com/lkxed
|
||||
[1]: https://opensource.com/article/22/8/math-python-raspberry-pi
|
||||
[2]: https://codewith.mu/
|
||||
[3]: https://opensource.com/article/20/6/open-source-alternatives-vs-code
|
||||
[4]: https://github.com/VSCodium/vscodium/releases
|
||||
[5]: https://flathub.org/apps/details/com.vscodium.codium
|
||||
[6]: https://opensource.com/sites/default/files/2022-10/codium-extension-python.webp
|
||||
[7]: https://open-vsx.org/
|
Loading…
Reference in New Issue
Block a user