Virtual Environment setup

Virtual environment (venv shortly) is a python mechanism where it is possible to create a space for your project and specific packages required by it.

Cases when Virtual Environment is useful

  • when you want to develop a project which will require specific version of libraries and you don’t want to mess with version which your python is using globally;
  • when you want to test your project with cooperation with different python versions and packages version
    • especially when from different reasons your target environment forces you to use specific python version or packages version
      • for example when you are developing module for legacy system
  • due to cleaning reasons – with venv you can just move whole folder with virtual env to the OS trash space and that’s it – to remove all dependencies you don’t have to manually uninstall each library
  • portability – you can easily

Working model with virtual environment

  1. (only once) installation of virtual environment
  2. activation
  3. usage (calling the code like normal usage)
  4. deactivation

During daily work it is enough to perform steps 2-3-4;

Actions to install virtual environment

  1. create new folder for venv
create new folder for your virtual env
create new folder for your virtual env

2. open command line or powershell (or shell on Linux / macOS) and run following commands:

  • go to folder above your virtual environment

cd <path>

  • establish new virtual environment

python -m venv .\my_venv\

After starting this command you should wait about 10 sec to execute.

When finished content of your folder will look like this:

new virtual environment after creating
new virtual environment after creating

  • perform activation of virtual environment – depending on OS it can be performed in following ways:
    • Linux
      • source my_venv/bin/activate
    • Windows
      • my_venv\Scripts\activate

Since now your command prompt will have prefix with the name of python environment. This will inform you that you are working on virtual environment.

Your command prompt will look like this:

command line (here Powershell) after activation of virtual environment
command line (here Powershell) after activation of virtual environment

3. Now you can start to work on jour project – just create own folder (for example MyNewProject) in the path of your virtual environment – like here:

creating own path inside virtual environment
creating own path inside virtual environment

Inside your project you can develop your application, install web framework use git version control system, etc.

  • You can see list of currently installed packages in your virtual environment over command:

pip list

  • If you want to install new libraries – just run:

pip install <your package name>

! be sure that your virtual environment is active – otherwise libraries will be installed globally

4. After finishing your work with virtual environment – just call following command:

deactivate

That’s it regarding working with virtual environment. enjoy!

Python GUI Kivy Tutorial – part 1 – install Kivy library and demo running

Kivy Logo

In order to create python application with user interface it is needed to use right libraries (packages). 

One of them is Kivy. Huge advantage of this package is that it is cross-platform. So programs written with it can be run on different operating systems.

Continue reading “Python GUI Kivy Tutorial – part 1 – install Kivy library and demo running”