#RaspberryPi – Running a #python script in a Python Virtual Environment on reboot / startup

Hi!

Adrian Rosebrock is a very smart person who has tons of great resources about Computer Vision in PyImageSearch.com. Most of them are with Python, and he also have some of them focused on how to perform CV using OpenCV in a Raspberry Pi.

In the post [Running a Python + OpenCV script on reboot, see resources] he explains how to automatically run a Python script when a Raspberry Pi starts. He uses python virtual environments, so the first 2 commands are focused on to load the virtual env. Then, move to the app folder and run the python script.

source ~/.profile
workon cv
cd /home/pi/pi-face-recognition
python startup.py

Something like this:

python source and cv on raspberry pi.

The suggested approach consists on create a Schell Script [.sh file] with these lines and add them to the auto start. However, once you create the file and test it, there seems to be an issue with the Source command.

python source not working on SH file

Ok, so no source command in an SH file. I started to think on install all my python dependencies directly in the main user, however the idea of working with virtual environments is very useful for me. It was to read online about Linux, python and more.

Note: Before moving forward, I may need to add some context. I need to run my python script in a Terminal. My device will always auto-start with a 3.5 inches touch screen and a camera, so I need some GUI loaded.

This is an excellent article on how to add actions to the Raspberry Pi start-up [How to Execute a Script at Startup on the Raspberry Pi, see resources]. So I added my SH file here and it didn’t work and I need to figure out how to load a virtual environment and run a python script.

After a couple of tests, I realized that all the files I need are part of the virtual env location in the device.

raspberry pi python folder for virtual envs

So, I only need to add the full path to my command to make it work without the and [workon] command. My complete command will became:

/home/pi/.virtualenvs/cv/bin/python /home/pi/pi-face-recognition/startup.py

So, I edited my autostart file adding this command

raspberry pi auto start file launching python script with a virtual env

And done! My python script running on a python virtual environment on the device startup is working!

Happy coding!

Greetings @ Toronto

El Bruno

Resources

Advertisements

VS2017 – Improving the solution load time

Hello!

Today I found another new cool feature in Visual Studio 2017. This one is related to the load time in “big solutions”. I had read about this, but it had not checked this out it and just today, when I saw the following message, had the interest to read about this feature.

clipboard04

Load <Solution Name> faster next time by trying out lightweight Solution Load. Some IDE features and extensions may not be available.

As well explained in this Visual Studio post (link), it is possible to change the default behavior for solution inside Visual Studio options

clipboard05

Or, on overwriting the global settings of the IDE, since the properties of the solution

Clipboard07.jpg

When we enable this “LightWeight Load” mode, Visual Studio will disable some features until we need them for 1st time, and thus the solution will load faster.

For example:

  • The projects are loaded as they are needed.
  • NuGet packages restore is not activated during the build.
  • There is no background solution prebuilt, so the design of XAML view are not available.

The 1st option is related to property already handling Visual Studio from making several versions: “LoadIfNeeded”. On this subject, I already wrote a post a while ago, and in it he reviewed a very useful extension for these scenarios.

Greetings @ Toronto

El Bruno

References

#VS2017 – Mejorando el tiempo de carga en nuestras soluciones

Hola !

Hoy me encuentro con otra novedad en Visual Studio 2017 relacionada con el tiempo de carga de soluciones “grandes”.  Algo había leído al respecto, pero no lo había registrado y recién hoy, cuando vi el siguiente mensaje, tuve la inquietud de volver a leer sobre esta feature.

clipboard04

Load <Solution Name> faster next time by trying out lightweight Solution Load. Some IDE features and extensions may not be available.

Como bien explican en este post de Visual Studio (link), es posible habilitar de manera global el proceso de carga de las soluciones desde las opciones de Visual Studio

clipboard05

O, sobre escriiendo las settings globales del IDE, desde las propiedades de la solución

Clipboard07.jpg

Cuando habilitamos este modo “LightWeight Load”, Visual Studio desactivará algunas funcionalidades hasta que las necesitemos por 1ra vez, y de esta forma la solución se cargará más rápido.

Por ejemplo:

  • Los proyectos se cargan a medida que se necesitan los mismos.
  • No se activa el restore de packages NuGet durante las build.
  • Al no precompilar en background la solución, las vistas de diseño de XAML no están disponibles.

La 1ra opción está relacionada con la propiedad que ya manejaba Visual Studio desde hacer varias versiones : “LoadIfNeeded“. Sobre este tema, ya escribí un post hace un tiempo, y en el mismo repasa una extensión muy útil para estos escenarios.

Saludos @ Toronto

El Bruno

References