在 VS Code 和 Codium 中编写 Python 程序
Python Codium
![](/data/attachment/album/202211/17/100909py38rj0tqxlyrq0t.jpg)



> 
> 如果你正在寻找一个优秀的、通用的、开源的、带有 Python 集成的代码编辑器，那么你可以尝试一下 Codium。
> 
> 
> 


在过去几年内，我有幸和中学生们一起，并带他们入门 [Python 开发](https://opensource.com/article/22/8/math-python-raspberry-pi) 和树莓派 400。这一切都很有趣，树莓派对于学生和我来说都是一个很好的平台。我们使用了 [Code with Mu](https://codewith.mu/)，并且一切都很成功。我们的 Python 技能随着经验的增长而增长，因此最近我开始寻找给这些学生提供更多东西的方法。


我参与了一个 Python 课程并在课程中接触了微软的 Visual Studio Code。我在课程中学到了很多关于如何为 Python 设置虚拟环境，以及如何为 Python 编程配置 VS Code 的知识。在学习过程中，我也认识了 [Codium](https://opensource.com/article/20/6/open-source-alternatives-vs-code)，它基本上是没有微软品牌和遥测的 VS Code。


如果你正在寻找一个优秀的、通用的、开源的、带有 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](https://github.com/VSCodium/vscodium/releases)。在 Linux 上，你可以下载一个包并使用你的包管理器安装它，或者 [使用 Flatpak](https://flathub.org/apps/details/com.vscodium.codium)。


在安装好 Codium 之后，打开你的应用程序或活动菜单，输入 `code` 以启动它。


### 安装 VS Code Python 扩展


代码其实不是什么特别的东西。它只是一些其他应用程序（编译器或运行时）解释的纯文本。你可以在 Codium 中编写 Python 代码而不需要特殊的扩展。但是，有一个 Python 扩展可以为你带来一些方便的功能。


点击“<ruby> 文件 <rt>  File </rt></ruby>”菜单，选择“<ruby> 首选项 <rt>  Preferences </rt></ruby>”，然后选择“<ruby> 扩展 <rt>  Extensions </rt></ruby>”。在“<ruby> 扩展 <rt>  Extensions </rt></ruby>”面板中，搜索 Python IntelliSense 扩展。


![VS Code 和 Codium 都有一个扩展管理器，它会在页面左侧打开，允许你安装附加模块](/data/attachment/album/202211/17/100932lzbgu5frgf5gtwzd.jpg)


你已经在 Codium 中设置了 Python。剩下的就是把它用起来。


### 为 VS Code 或 Codium 设置虚拟环境


我们可以创建一个项目目录，并将其添加到 Codium 中，这样在工作时，你创建和保存的文件都将默认保存到活动项目目录。这是一种快速的管理方式，可以让你不必经常点击文件保存和打开对话框。


在你创建一个虚拟 Python 环境作为工作目录时，Codium 会检测到它（因为你已经安装了 Python 扩展）。当你激活一个虚拟环境文件夹作为活动项目目录时，Codium 会自动运行使用虚拟环境所需的激活代码。


要为 Python 创建一个虚拟环境，请打开终端并输入：



```
$ python3 -m venv ~/PythonCoding

```

### 添加项目目录


在 Codium 中，点击“<ruby> 文件 <rt>  File </rt></ruby>”菜单，选择“<ruby> 将文件夹添加到工作区 <rt>  Add Folder to Workspace </rt></ruby>”。打开你刚刚设置的虚拟环境（对我来说，是 `/home/don/PythonCoding`）。


现在你已经准备好写一些 Python 代码了！在你的工作区中创建一个新的 Python 文件并插入一些基本代码。当你输入时，你可能会注意到，Codium 会为环境包含的 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](https://open-vsx.org/) 中找到并安装其他扩展，这是一个中立的开源 VS Code 扩展 “市场”。


Codium 的界面比一些基本的编辑器更复杂，但它有我在学习过程中所需要的东西。如果你需要一个更专业的编辑器，或者你想从当前的编辑器切换到新的编辑器，那么试试 Codium 吧。




---


via: <https://opensource.com/article/22/11/python-vs-code-codium>


作者：[Don Watkins](https://opensource.com/users/don-watkins) 选题：[lkxed](https://github.com/lkxed) 译者：[Cubik65536](https://github.com/Cubik65536) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Over the past couple of years, I have had the privilege of working with middle school children to introduce them to [Python coding](https://opensource.com/article/22/8/math-python-raspberry-pi) 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](https://codewith.mu/) 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](https://opensource.com/article/20/6/open-source-alternatives-vs-code), 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](https://github.com/VSCodium/vscodium/releases) on your computer. On Linux, you can download a package and install it with your package manager, or [use the Flatpak](https://flathub.org/apps/details/com.vscodium.codium).

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.](https://opensource.com/sites/default/files/2022-10/codium-extension-python.webp)

(Opensource.com, CC BY-SA 4.0)

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.

```
``````
import sys
print ("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](https://open-vsx.org/), 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.

## Comments are closed.