Upgrading Your Home Assistant Core Python Virtual Environment

Home Assistant Core Python Virtual Environment Pin

Since Python 3.9 is available, Home Assistant announced they were going no longer support Python versions lower than 3.8. This happens from time to time. If you run Home Assistant Core in a Python virtual environment, then you need to take action to maintain compatibility. I always forget these steps, so I decided to write an article I could always refer to.

Step 1: Install new version of Python

The first step is to install the new version of Python. How you do this will depend on what OS you are running. Many are woefully behind in their default Python versions. I run Ubuntu, and all my commands will be with Ubuntu in mind, but are easily adaptable to other distros. You check your version of Python by running the following commands:

python --version
python3 --version

You’ll probably be running an older version of Python than you want. You can check your repos to see if there is an up to date version. If so, you can install it directly from the repos. Otherwise, Python is pretty easy to compile and install from source. If you wanted to install Python 3.8.5 you would do the following:

wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz
tar -xzf Python-3.8.5
cd Python-3.8.5
./configure
make -j 4
sudo make install

Depending on the speed of your system, this could take a little time, but should be straight forward.

See also  Wiring the Shelly Plus 1 Relay

Step 2: Backup your current Python packages

I don’t know if this step is strictly necessary to upgrading your Python virtual environment, but it is certainly prudent, especially if you made some changes and customization to Home Assistant that involved Python packages (like custom components). Assuming your Home Assistant config is in /home/homeassistant/.homeassistant and your Python virtual environment is in /srv/homeassistant:

cd /home/homeassistant/.homeassistant
sudo -u homeassistant -H -s
source /srv/homeassistant/bin/activate
pip3 freeze –local > requirements.txt

This will put a listing of all your python packages in the file requirements.txt. You might want to take a peek at the file to make sure it looks right.

Step 3: Remove old Python virtual environment and create a new one

The first time I did this it was the scariest part. Instead of deleting the old python virtual environment, I recommend moving/renaming it, just in case. I recommend you stop Home Assistant at this point as well, although I don’t know that it is required. Also, make sure you are no longer in the venv (exit) before running these commands:

sudo systemctl stop home-assistant@homeassistant 
cd /srv/
sudo mv homeassistant homeassistantold
sudo mkdir homeassistant
sudo chown homeassistant:homeassistant homeassistant
sudo -u homeassistant -H -s
cd /srv/homeassistant
python3.8 -m venv .

Step 4: Install packages and Home Assistant into Python virtual environment

You are almost done. This is possibly an optional step, but a couple of times in the past I’ve needed to install python packages when I haven’t done this. Use the requirements.txt file you created earlier to reinstall all the packages you had installed in the previous virtual environment.

source /srv/homeassistant/bin/activate
cd /home/homeassistant/.homeassistant
pip3 install -r requirements.txt

Installing the packages might take a while and you may see some errors or failures, but generally, you don’t have to worry about them (I didn’t and things have always worked fine). Now install and start Home Assistant:

pip3 install homeassistant
exit #exit the venv
sudo systemctl start home-assistant@homeassistant

Final thoughts

That’s it. Hopefully, it all goes well. It has for me the couple of times I’ve had to do it. If you’ve decided to run Home Assistant Core in a Python virtual environment you’ll probably have to do this procedure about once a year.

See also  The Evolution Of My Home Assistant Setup
Interested in supporting HomeTechHacker?

Have you found the content on this site useful? If so, are you interested in supporting me and this site? There’s no obligation of course, but I would really appreciate any support you can give. Below are a few ways you can show support:


Thank you! I really appreciate it!
Share this:

2 thoughts on “Upgrading Your Home Assistant Core Python Virtual Environment”

Comments are closed.

Upgrading Your Home Assistant Core Python Virtual Environment

by HomeTechHacker time to read: 2 min