命令 'python' 未找到 - 在我将其作为别名分配给 python3 之后

Command 'python' not found - after I've assigned it as an alias to python3

我正处于使用 python 学习 Django 的早期阶段。之前都做过 none。

我正在学习一门课程,讲师展示了如何使用 vag运行t 开始一个新项目,主目录中的一个文件名为 Vagrantfile,没有文件扩展名(与讲师相同)。

文件内容如下:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
 # The most common configuration options are documented and commented below.
 # For a complete reference, please see the online documentation at
 # https://docs.vagrantup.com.

 # Every Vagrant development environment requires a box. You can search for
 # boxes at https://vagrantcloud.com/search.
 config.vm.box = "ubuntu/bionic64"
 config.vm.box_version = "~> 20200304.0.0"

 config.vm.network "forwarded_port", guest: 8000, host: 8000

 config.vm.provision "shell", inline: <<-SHELL
   systemctl disable apt-daily.service
   systemctl disable apt-daily.timer

   sudo apt-get update
   sudo apt-get install -y python3-venv zip
   touch /home/vagrant/.bash_aliases
   if ! grep -q PYTHON_ALIAS_ADDED /home/vagrant/.bash_aliases; then
     echo "# PYTHON_ALIAS_ADDED" >> /home/vagrant/.bash_aliases
     echo "alias python='python3'" >> /home/vagrant/.bash_aliases
   fi
 SHELL
end

根据我的理解,这一行echo "alias python='python3'" >> /home/vagrant/.bash_aliases告诉vag运行t,当我使用python命令时,它应该认为我写了python3

当我通过 python -m venv ~/env 编写以下命令时,我收到以下消息作为响应:

Command 'python' not found, but can be installed with:

apt install python3
apt install python
apt install python-minimal

Ask your administrator to install one of them.

我在 运行 vagrant ssh 命令之后调用此命令并且我已经在虚拟机中,然后导航到 vag运行t 目录。我知道它是因为在 git bash 中这显示为我当前的位置 vagrant@ubuntu-bionic:/vagrant$.

我不确定它是否相关,或者它是否是一个不同的问题,但我会补充一点,即使我 运行 这个命令而不是 python3 -m venv ~/env,我得到这个回应:

The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/home/vagrant/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

如果我运行它推荐的安装命令(使用sudo),我会得到这样的回应:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Package python3-venv is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

这阻碍了我的学习,不知道如何继续他的学习。感谢任何帮助。

问题可能是将 python3 别名为 python,默认情况下 Ubuntu 18 同时发货 Python 2 (python) 和 Python 3 (python3) 粗心地更改它会导致不稳定,从细微的错误到系统无法启动,您可以:

  • 使用update-alternatives改变它
  • 按原样使用python3
  • 安装包python-is-python3(虽然我不确定这在 18 中是否可用)

无论如何,通过删除别名,一切都会恢复正常。

我正在尝试在 Ubuntu 20.04.

上重现此内容
sudo dpkg -i virtualbox-6.1_6.1.14-140239~Ubuntu~eoan_amd64.deb
mkdir vagrant  
cd vagrant  
vagrant init hashicorp/bionic64
vagrant up  

此时我的 Vagrantfile 不包含别名。参见 https://github.com/michaelhochleitner/vagrant-test/blob/master/Vagrantfile

在没有别名的 vagrant ssh 中测试 python。

vagrant ssh
python
Python 2.7.15+ (default, Nov 27 2018, 23:36:35) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

正在使用别名在 vagrant ssh 中测试 python。 此时我的 Vagrantfile 包含来自您的 post 的别名。参见 https://github.com/michaelhochleitner/vagrant-test/blob/master/Vagrantfile_with_alias

vagrant reload
vagrant ssh  
python
Python 2.7.15+ (default, Nov 27 2018, 23:36:35) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

我假设

Command 'python' not found, but can be installed with:

错误不是由 Vagrantfile 中的别名引起的。

尝试通过安装 python3-venv.
来解决问题 在 vagrant ssh 之外的终端中 python 的输出是什么?
在 vagrant ssh 之外的终端中 python3 的输出是什么?
您使用的是哪种操作系统? 另见 https://askubuntu.com/questions/958303/unable-to-create-virtual-environment-with-python-3-6.