Centos7, Python3

This might be useful for anyone else using Centos or Redhat, which come with Python 2 by default.

If you’re not aware, you don’t need to compile Python3 for yourself to use it. If you enable software collections you can install later versions - see the site, which even gives Python 3.5 as an example.

To run under this config, you’d run (as the homeassistant user):

scl enable rh-python bash

Then create your virtual environment - as Python3 is now your default you don’t have to specify:

virtualenv -p python venv
source ./venv/bin/activate

You can then pip install as normal within that environment (including dependencies - I had to pip install netifaces separately for some reason)

It took me a few goes to get it working in the systemd script. You need to provide the Environment for your virtualenv as usual but then change your ExecStart:

[Unit]
Description=Home Assistant
After=network.target

[Service]
Type=simple
User=homeassistant
Environment=VIRTUAL_ENV=“/path/to/venv”
Environment=PATH=“$VIRTUAL_ENV/bin:$PATH”
ExecStart=/usr/bin/scl enable rh-python35 – /path/to/venv/bin/hass -c “/path/to/config”

[Install]
WantedBy=multi-user.target

This may be obvious if you’ve used software collections before, but it took me a while and hopefully someone will find this useful :slight_smile:

2 Likes

How did you install Cython?

In the virtual enviroment i ran pip install Cython and got the following error
I installed the base package outside of VE
Cython.x86_64 : A language for writing Python extension modules

error: could not create '/opt/rh/rh-python34/root/usr/lib64/python3.4/site-packages/cython.py': Permission denied

----------------------------------------
Cleaning up...
Command /opt/rh/rh-python34/root/usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-9u64p_n7/Cython/setup.py';ex                                         ec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-swk48                                         2nu-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip-build-9u64p_n7/Cython
Storing debug log for failure in /tmp/tmpzy7zy8z_

If you’re trying to install packages outside of your virtual environment, you need to be root (or use sudo)

I’d recommend doing everything within the virtual environment. Enable the software collection and the virtual environment and you should be able to run:

pip3 install --upgrade cython==0.24.1

If you’re doing this for Zwave, you can follow the instructions but you shouldn’t need the PYTHON_EXEC variable - the software collection will have set the correct python version. You also shouldn’t need the sudo for the make install step - if you just install, it will install in the virtual environment’s site-packages:

(venv)$ git clone https://github.com/OpenZWave/python-openzwave.git
(venv)$ cd python-openzwave
(venv)$ git checkout python3
(venv)$ make build
(venv)$ make install
(venv)$ cd ..
(venv)$ find . -name 'libopenzwave.*'
./lib/python3.5/site-packages/libopenzwave.egg-link
./lib/python3.5/site-packages/libopenzwave-0.3.2-py3.5-linux-x86_64.egg/libopenzwave.cpython-35m-x86_64-linux-gnu.so
./lib/python3.5/site-packages/libopenzwave-0.3.2-py3.5-linux-x86_64.egg/libopenzwave.py
./lib/python3.5/site-packages/libopenzwave-0.3.2-py3.5-linux-x86_64.egg/__pycache__/libopenzwave.cpython-35.pyc
(venv)bash-4.2$ 

So everything is now installed within your virtual environment.

It took me a few attempts to get it right, and this is piecing together what I did from looking at the command history, so I’m hoping I haven’t missed any steps there…