从终端运行python_如何从终端运行不同版本的python

从终端运行python

为了安装我需要的软件包,我需要切换到旧版本的Python,但仅适用于该项目。 这是简单的解决方案:版本特定的虚拟环境。 (In order to install the package I required, I needed to switch to an older version of Python, but only for that project. Here is the simple solution: Version specific virtual environments.)

As of writing this, I am using the latest version of Python on my machine: 3.8.5. It is usually fine to use the latest version. Except every now and then, something juicy requires an earlier version of Python for it to work, or in some cases, it won’t even install without that older version.

在撰写本文时,我正在计算机上使用最新版本的Python:3.8.5。 通常可以使用最新版本。 除时不时地进行之外,某些功能还需要较早版本的Python才能运行,或者在某些情况下,如果没有该较旧版本,它甚至无法安装。

In this case, I’m talking about Kivy. It’s a super slick front end app development library for Python. It lets you develop mobile apps for your device completely in Python, and it’s really solid from what I can see (it even supports Material Design). But for you, it could be anything that doesn’t have support for your version of Python. Here’s a really simple and elegant way to get your specific version up and running.

在这种情况下,我说的是Kivy 。 这是一个用于Python的超级漂亮的前端应用程序开发库。 它使您可以完全使用Python为设备开发移动应用程序,并且据我所知(它甚至支持Material Design )确实很可靠。 但是对您而言,可能是不支持您的Python版本的任何内容。 这是一种非常简单而优雅的方式来启动并运行您的特定版本。

  1. Install it on your system. Ok, you can’t use a specific version if it doesn’t exist on your system. So install the specific Python version you need. In this case, it was Python 3.7 and I already had it on my machine.

    将其安装在系统上。 好的,如果您的系统上不存在特定版本,则无法使用。 因此, 请安装所需的特定Python版本 。 在这种情况下,它是Python 3.7,而我已经在计算机上安装了它。

  2. Next, find or create a symlink to it. When you type python in terminal, press tab before you press enter to see what you have available. You’ll see the symlinks (like shortcuts) to the different versions of Python that you have already.

    接下来,找到或创建一个符号链接 。 在终端中键入python时,请按tab键,然后按Enter键以查看可用的内容。 您将看到指向不同版本Python的符号链接 (如快捷方式)。

  3. If you see your python version in the list, skip to step 6. If not, you need to find out where it’s installed.

    如果您在列表中看到python版本,请跳至步骤6。否则,您需要找出它的安装位置。

  4. I had a version of Python 3.7 installed in /usr/bin/python3.

    我在/usr/bin/python3安装了Python 3.7版本。

    (I figured this out from VSCode, when I went to change my python interpreter by selecting this little button here, it showed me the path).

    (我从VSCode中了解了这一点,当我通过选择此处的小按钮来更改python解释器时,它向我显示了路径)。

A view of Microsoft VS Code showing a list of available Python interpreters and their locations.
Python: Select Interpreter” from the command pallete. Python:Select Interpreter”)进入此列表。

5. So I created a symlink to it using the command:

5. 因此,我使用以下命令创建了一个 符号链接

ln -s /usr/bin/python3 /usr/local/bin/python37

Why did I need a symlink? For starters, a symlink lets me run Python 3.7 any time I want by typing in python37, but secondly, I wanted to be able to easily use it in my virtual environment.

为什么需要符号链接? 对于初学者来说,使用symlink可以通过键入python37随时运行Python 3.7,但其次,我希望能够在虚拟环境中轻松使用它。

If you don’t know about virtual environments, check my article on what they are and how to easily create one.

如果您不了解虚拟环境, 请查看我的文章以了解它们是什么以及如何轻松创建虚拟环境。

6. Now that I had my symlink, I simply navigated to my project folder that needed python python 3.7, and created my virtual environment, except this time, with the -p tag which allowed me to specify my python version for anything in this venv instance. Type:

6.现在有了我的符号链接,我只需导航到需要python python 3.7的项目文件夹,并使用-p标签创建虚拟环境(这次除外),这允许我为此venv中的任何内容指定python版本实例。 类型:

virtualenv venv -p python37

7. Next, you want to activate your new virtual environment so, make sure you’re in the parent folder and type:

7.接下来,您要激活新的虚拟环境,因此,请确保您位于父文件夹中,然后键入:

# For Unix/Linux based, type:
source venv/bin/activate# For Windows, type:
. venv/Scripts/activate

8. Now type python --version to check if it worked. While you’re inside your virtual environment, you can type python, rather than your new symlink python37, because it is your primary version for all projects in this environment.

8.现在键入python --version来检查它是否有效。 在虚拟环境中时,可以键入python ,而不是新的symlink python37 ,因为它是该环境中所有项目的主要版本。

9. From this point, I was able to install Kivy without getting any weird errors, and everything worked great.

9.至此,我能够安装Kivy而不会出现任何奇怪的错误,并且一切正常。

而已! (That’s it!)

Hopefully this helps you if you ever need to specify a particular version of Python for a project. Answer forums are great, but sometimes it can become a real treasure hunt trying to find the right answers.

如果您需要为项目指定特定版本的Python,希望这对您有所帮助。 答案论坛很棒,但有时它可以成为试图找到正确答案的真正的寻宝游戏。

Happy coding!

编码愉快!

有用的提示 (Helpful tips)

  1. If you want to exit your virtual environment, just type the word deactivate.

    如果要退出虚拟环境,只需键入单词deactivate.

  2. If your desired symlink already exists, type unlink <symlink path> and it will free it up for you.

    如果所需的符号链接已经存在,请键入unlink <symlink path> ,它将为您释放它。

翻译自: https://medium.com/swlh/how-to-run-a-different-version-of-python-from-your-terminal-fe744276ff22

从终端运行python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值