I’m in the early stages of learning django with python. Have done none of both before.
I’m following a course where the instructor has showed how to start a new project using vagrant, and one of the files in the main directory is called Vagrantfile
, no file extension (same for the instructor).
These are the contents of the file:
# -*- 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
From my understanding, this line echo "alias python='python3'" >> /home/vagrant/.bash_aliases
tells vagrant that when I use the python
command, it should regard it as if I wrote python3
.
When I write the following command though python -m venv ~/env
, I get in response this message:
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.
I call this command after I’ve ran the vagrant ssh
command and I’m already inside the virtual machine, and navigate to the vagrant dir. I know it because in git bash this shows as my current location vagrant@ubuntu-bionic:/vagrant$
.
I’m not sure if it’s related, or if it’s a different issue, but I’ll add that even if I run this command instead python3 -m venv ~/env
, I get this response:
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']
If I run the install command it recommends (with sudo
), I get this in response:
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
This blocks my learning, no idea how to move forward with his. Any help is appreciated.
2
Answers
The problem is probably aliasing
python3
topython
, by default Ubuntu 18 shipped both Python 2 (python) and Python 3 (python3) and changing that carelessly can cause instabilities, ranging from subtle bugs, to your system not booting, you can:update-alternatives
to change itpython3
as it ispython-is-python3
(although I’m not sure if this is available in 18)Anyway, by deleting the alias, everything should go back to normal.
I’m trying to reproduce this on Ubuntu 20.04.
At this point my Vagrantfile contains no alias. See https://github.com/michaelhochleitner/vagrant-test/blob/master/Vagrantfile.
Testing python in vagrant ssh without alias.
Testing python in vagrant ssh with alias.
At this point my Vagrantfile contains the alias from your post. See https://github.com/michaelhochleitner/vagrant-test/blob/master/Vagrantfile_with_alias.
I’m assuming the
error is not caused by the alias in the Vagrantfile.
Try to resolve the issue by installing python3-venv.
What is the output of
python
in a terminal outside vagrant ssh?What is the output of
python3
in a terminal outside vagrant ssh?Which operating system are you using?
Also see https://askubuntu.com/questions/958303/unable-to-create-virtual-environment-with-python-3-6.