在我将命令“python”指定为python3的别名后,未找到该命令

2024-04-23 18:34:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正处于使用python学习django的早期阶段。我以前两样都没做过

我正在学习一门课程,其中讲师演示了如何使用vagrant启动一个新项目,主目录中的一个文件名为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告诉vagrant,当我使用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命令并且已经在虚拟机中之后调用该命令,并导航到vagrant dir。我知道它是因为在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']

如果我运行它推荐的install命令(使用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

这阻碍了我的学习,不知道如何与他一起前进。感谢您的帮助


Tags: installthe命令bashconfigpackagehomevenv
2条回答

我正试图在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  

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

在无别名的浮动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。 此时,我的文件包含了您帖子中的别名。见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:

错误不是由文件中的别名引起的

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

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

  • 使用^{}更改它
  • 按原样使用python3
  • 安装程序包python-is-python3(尽管我不确定这是否在18中可用)

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

相关问题 更多 >