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  The Current State of My Smart Home

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:

sudo systemctl stop home-assistant@homeassistant 
cd /srv/
sudo mv homeassistant homeassistandold
python3.8 -m venv homeassistant

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

You are almost done. Use the requirements.txt file you created earlier to reinstall all the packages you had installed in the previous virtual environment. Then, install Home Assistant.

sudo -u homeassistant -H -s
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:

python3 -m pip install homeassistant
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  Planning My Holiday LEDs Early
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:

  • Share this site with your friends and on social media (use the sharing links at the end of this page for your convenience)
  • Subscribe to this site
  • Purchase my book, The Smart Home Manual, for yourself or as a gift
  • Put a link to HomeTechHacker on a site you have access to. Be sure to let me know about it!
  • Reach out to me via my contact page or Twitter and let me know something I should write about
  • Shop at Amazon through my affiliate links and ads on these pages. See my disclosures for more details about affiliate links. You can also just shop from one of the links below:
    1. HomeTechHacker Shop: This is a listing of products that I use, have reviewed, and that I recommend
    2. HomeTechHacker Technology Advisor: This suite of tools will give you customized home technology product recommendations based on your needs
    3. My US Amazon affiliate link: Just click on this link to go to Amazon and shop

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