#Windows10- #WindowsTerminal and Anaconda, bonus: start an Anaconda Virtual Environment

Buy Me A Coffee

Hi !

Windows Terminal (WT) is one of the coolest tools I’ve using in the last couple of years. I’m not an expert, and not even a fan of CLIs, however I assume working with WT is super cool.

Bonus: If you speak Spanish, I shared my own thoughts about this with Juan and Eduard in a podcast episode here.

On top of this, I also use Anaconda a lot. And, now that we can launch and use Anaconda from a PowerShell Prompt, I think I should spend some time trying to figure out how to have Anaconda inside Windows Terminal.

windows terminal new profiles for Anaconda

I will assume that you know the basis of Windows Terminal profiles. As a WT user, we can create as many profiles as we want to have different tools available. This is my starting point to use Anaconda and Windows Terminal.

Note: Check References for Donovan Brown post about working with profiles.

Create a new profile to launch Anaconda in Windows Terminal

Let’s go to Windows Terminal Settings to create a new profile for Anaconda. In order to do this, I’ll copy and paste an existing profile, update the Guid and complete the following values.

  • guid: create and paste a new Guid
  • name: I defaulted this to Anaconda
  • commandline: this is the tricky one. So I’ll describe the steps below.

I browse to [C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit)] and view the properties for the [Anaconda PowerShell Prompt]. Then copy the Target value and use the value in the commandline element.

get properties from shortcut for Anaconda Command Prompt

That should be enough to have a new profile to work with Anaconda. When we open a new tab for Anaconda, we can see that it open the standard virtual environment (base).

default anaconda prompt in windows terminal

1st step done, have Anaconda integrated with Windows Terminal !

Start an Anaconda Virtual Environment in a new Windows Terminal Tab

Moving on, I’ll create and activate a new Virtual Environment named [drone], with the following commands.

conda create -n drone pip python=3.7
conda activate drone

Now I have a new environment named [drone], and I want to have a shortcut in Windows Terminal to open a new tab with this VirtualEnv activated. I copy & paste the definition of the Anaconda profile, used a new Guid, and added, the following command to the end of the line:

conda activate drone
virutal env openend in windows terminal

As you can see in the previous image, when I open a new tab for my Drone Virtual Env, I already have it loaded. I also added a [cls] command at the end, so I can start with a clean environment.

Finally, and for reference, this is my current Windows Terminal settings file including the 2 Anaconda profiles.

Happy coding!

Greetings

El Bruno

References

#RaspberryPi – Install Virtual Environments

Buy Me A Coffee

Hi!

Virtual Environments are a great way to isolate our dev tests, and after been using this in Windows also, work with them in the Raspberry Pi makes a lot of sense.

I’ll leave here the necessary steps to do this, however the full credit is based on some posts from Adrian Rosebrock and his amazing blog (see references).

Once we have everything updated in our Raspbian, let’s run the following command:

sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/get-pip.py ~/.cache/pip

Now we need to update the ~/.bashrc file using nano (not a big fan of VI )

nano ~/.bashrc

And then add the following lines

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

Now everytime we open a new terminal session, this commands will be applied and we will have our virtual environments up and running.

Next, source the bashrc file

source ~/.bashrc

And we can create a new virtual environment. Let’s create one named devOpenCV using python 3.

mkvirtualenv devOpenCV -p python3

We can enable and access the virtual environment with the workon command, and we will see the virtual environment as a prefix in our terminal

workon devOpenCV

As we can see in the following screenshot, the virtual environment uses the latest Python 3 version, and just a few packages installed.

Happy coding!

Greetings

El Bruno

References

My posts on Raspberry Pi

Dev posts for Raspberry Pi
Tools and Apps for Raspberry Pi
Setup the device
Hardware

#RaspberryPi – Install OpenCV

Buy Me A Coffee

Hi!

After some posts about how to setup a Raspberry Pi, today I’ll share the steps I follow to install OpenCV.

Disclaimer: if you are looking for a detailed step by step on how to install or even build OpenCV in a Raspberry Pi, I strongly recommend to read the post “Install OpenCV on Raspberry Pi 4” by Adrian Rosebrock.

Ok, so let’s start. I assume that you read my posts and your Raspbian image is up and running.

Install Python 3 and Update device

1st step will be to install Python 3 with the following command

sudo apt-get install python3-dev

And run and update for all the installed software

sudo -- sh -c 'apt-get update; apt-get upgrade -y; apt-get dist-upgrade -y; apt-get autoremove -y; apt-get autoclean -y'
Install and use Virtual Environments

This will give us the base image to start working. And, in case we need to install different versions or different apps, I’ll use virtual environments to work with Python.

Let’s install VirtualEnv with the command

#create virtual environment
sudo pip3 install virtualenv

Now let’s create a new virtual environment named “venv” with the command

virtualenv -p python3 .venv

And let’s activate the environment with the command

source .venv/bin/activate

At this moment, the terminal should change and add a prefix (venv) in the bash.

raspberry pi install and activate a virtual environment
Install prerequisites

Let’s update again

sudo apt-get update

And install prerequisites with the commands

sudo apt-get install gfortran 
sudo apt-get install libopenblas-dev 
sudo apt-get install liblapack-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
sudo apt-get install libqtgui4
sudo apt-get install python3-pyqt5
sudo apt-get install libqt4-test

or in a single command

sudo apt-get install gfortran libopenblas-dev liblapack-dev libatlas-base-dev libjasper-dev libqtgui4 python3-pyqt5 libqt4-test -y

This process will take some minutes, so this is time 1 to get a coffee!

Install OpenCV and switch to right Raspberry Pi version!

And now the magic command to install OpenCV

sudo apt-get install libopencv-dev

And this process is the one who take most of the time, so coffee number 2. Take a look at all the dependencies for this

And after a couple of minutes the process is done. We can test the OpenCV version running 2 simple python commands. First let’s start python with the command

python

And then run the following lines

import cv2
cv2.__version__

This should display the current OpenCV version.

However, with the latest version we have an error: ModuleNotFoundError: No module named ‘cv2’

The current installed version have some issues running in the raspberry py, so we need to make a downgrade to the version 4.1.0.25 with the command. We first uninstall the installed version (4.1.0.26) and install the specific version.

pip uninstall opencv-contrib
pip install opencv-contrib-python==4.1.0.25

Now, we launch python again, run our 2 lines, and we got OpenCV up and running!

Bonus: Installed Packages

Finally, this is the current list of packages installed in the virtual environments and the version of each package

(.venv) pi@rpidev5:~ $ pip3 list
Package               Version
--------------------- --------
numpy                 1.18.1
opencv-contrib-python 4.1.0.25
pip                   20.0.1
setuptools            45.1.0
wheel                 0.33.6
(.venv) pi@rpidev5:~ $

Happy coding!

Greetings

El Bruno

References

My posts on Raspberry Pi

Dev posts for Raspberry Pi
Tools and Apps for Raspberry Pi
Setup the device
Hardware

#Anaconda – My steps to install a virtual environment with #TensorFlow, #Keras and more

Hi!

So today post is not a post, just a selfish reminder of the steps I do when I setup a new dev machine

  • Install Anaconda (see references). I use the default settings, and important: I don’t add Anaconda to Windows PATH.
  • Open Anaconda command prompt as administrator
open anaconda as administrator

Need to be open as Admin in order to install updates

  • Install updates with the command
conda update conda 
conda update –all
  • Create a new development environment named “tfEnv” with tensorflow. Activate the environment
conda create -n tfenv tensorflow 
conda activate tfenv
  • The command to install keras is
pip install
keras

However, if it doesn’t work, I install keras with the following packages

pip install matplotlib 
pip install pillow
pip install tensorflow==1.14
conda install mingw libpython
pip install git+git://github.com/Theano/Theano.git
pip install git+git://github.com/fchollet/keras.git
  • Finally, install Jupyter notebook kernel and create a new kernel for the current virtual environment
pip install ipykernel 
ipython kernel install --user --name=tfEnv
  • There seems to be an issue to install OpenCV using pip with the command
pip install
opencv-python

So, I Install the OpenCV nonofficial package. 1st I download a compatible package from

https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopencl

Install with

pip install
c:\temp\opencv_python-4.1.1-cp36-cp36m-win_amd64.whl

Happy coding!

Greetings @ Toronto

El Bruno

References

#Anaconda – How to create a custom #Python virtual environment and use it in #Jupyter notebooks (a kernel!)

Hi!

In yesterday post, I created a new virtual environment named [devtf] and in this environment I’ve installed a lot of tools that I need. Then I tried to launch a jupyter notebook from this environment, to use this tools and, of course, it didn’t work.

anaconda start virtual environment and error on launch jupyter notebook

It was time to read and learn how this works. So, when I finally get this I find this amazing article which really explain how this works “Using Virtual Environments in Jupyter Notebook and Python” (see references)

Jupyter Notebook makes sure that the IPython kernel is available, but you have to manually add a kernel with a different version of Python or a virtual environment. First, you need to activate your virtual environment. Next, install ipykernel which provides the IPython kernel for Jupyter. And finally, you can add your virtual environment to Jupyter.

So the commands are

pip install --user ipykernel 
python -m ipykernel install --user --name=devtf

Where “devtf” is the name of the new kernel you want to create. Now, when I launch Jupyter Notebooks, the new kernel is available to be used

jupyter notebook change kernel to one with tensorflow

When I started to use this new kernel (virtual environment) I realized that I didn’t installed TensorFlow. You know, being happy about this, naming the kernel TF but not installing the core component. And, sure, my notebooks didn’t work.

jupyter notebook with kernel without tensorflow

I went to my terminal / command prompt and installed TensorFlow. Then I only need to restart the Kernel, and everything start working. I added a extra couple of lines in my notebook just to check the TensorFlow and keras versions.

jupyter notebook tf ok and test keras version

I find similar errors with another packages, so I pip installed the packages in the terminal and restart the kernel to have the notebook OK. So, my simple reminder for myself about how to do this!

Happy coding!

Greetings @ Mississauga

El Bruno

References

#Python – How to create a Virtual Environment in #Windows10 (Easy one, and brain backup!)

Hi!

Quick post today, and mostly a reminder on how to create a Virtual Environment in Python in Windows 10. I’ve been doing this mostly in my Mac and my Raspberry Pi, and I always forget how to do this on Windows, so … I’ll write this post to have this

Install Python3.

Download the installers from the official Python source (see references). I usually install it on the root of my C: hard drive and name it with the version. In example: c:\Python37_64 folder.

Remember to also add this folder and the Scripts folder to the Environment Variables.

Note: Once you start to install tools which uses Python, your OS becomes a nightmare. You will have your own installed python, the version installed with Visual Studio, the one with Visual Studio Code, another one for Anaconda.

I’m not sure if this is a best practice or not, but I usually remove all the other versions and keep mines in the root of the C: drive.

Ok, let’s go on.

Install VirtualEnv

Let’s install virtualenv and the wrapper

pip install virtualenv virtualenvwrapper

Create new Virtual Environment

For a new virtual environment named “testEnv”, open a command prompt and navigate to the python folder. Then run the command

python -m virtualenv testEnv

After a couple of seconds, the virtual Environment will be installed, and you can use it by run the [activate.bat] script. In this example

  • The virtual environment will be created at [c:\Python37_64\testEnv]
  • The virtual environment activate script will be at [c:\Python37_64\testEnv\Scripts\activate.bat]

So the full command sequence is similar to this one

Now you have your virtual environment up and running and you may want to start to add your own packages or tools. Like in example: Numpy or to list the installed packages

Finally, you may leave the virtual environment with the command

deactivate

Happy Coding!

Greetings @ Toronto

El Bruno

Resources