To easily run Python, a virtual environment is recommended. For a more detailed description of what the virtual environment is, why it is needed and how to install it visit RealPython.com python virtual environments. Virtual environments are needed to help with storing directories and receiving site packages such as canlib.
Before creating a virtual environment, we need to create a file/directory where we want our virtual environment and projects that will use the environment. In this example the name of the file created is env.
Firstly, make a new directory and go into it using:
$ mkdir python-virtual-environments && cd python-virtual-environments
Then install the python virtual environment with:
$ sudo apt-get install python3-venv
To create a virtual environment in Linux run the following:
$ python3 -m venv .venv –prompt pyproj
“python3” dictates that Linux will launch the latest version of Python 3, “-m” dictates that we want to run a module and “venv” is the name of the module. “.venv” decides the name of the directory where the virtual environment will be created. “–prompt” changes the name of the virtual environment prompt when it is activated and “pyproj” dictates that the name should be “pyproj”.
To activate the virtual environment we need to run the following:
$ source .venv/bin/activate
(pyproj) $
This is a path that dictates the source through .venv to bin and lastly activate.
Now we can use the environment, its packages and resources in isolation. To deactivate the virtual environment, type:
(pyproj) $ deactivate