Creating virtual environments and installing libraries for Python 3 in the PyCharm IDE

The Python programming language is considered quite simple. It is easier and faster to write programs in comparison with compiled programming languages. For Python, there are many libraries that allow you to solve almost any problem. Of course, there are minuses and other nuances, but this is a separate issue.


Quite often, I see my friends and acquaintances begin to learn Python and face the problem of installing and using third-party libraries. They can spend several hours installing the library, and even, they can’t cope with it and score on it. While, in most cases, this could be done in a few minutes.


The article begins with basic things: installing Python 3, the Pip and Virtualenv development tools, and the PyCharm development environment on Windows and Ubuntu. For many, this is not difficult and it is possible that everything is already installed.


After what will be what the article was intended for, I will show how to create and use virtual environments in PyCharm and install libraries in them using Pip.


Install Python and Pip


Pip is a package manager for Python. It is with the help of it that modules / libraries for development in the form of packages are usually installed. On Windows, Pip can be installed through the standard Python installer. Ubuntu Pip is installed separately.


Install Python and Pip on Windows


For windows, go to the official download page , where then go to the download page of a specific version of Python. I'm using Python 3.6.8 , because LLVM 9 requires Python 3.6 installed.


Next, in the table with the files, select "Windows x86-64 executable installer" for a 64-bit system or "Windows x86 executable installer" for a 32-bit one. And run the downloaded installer, for example, for Python version 3.8.1 it is called python-3.8.1-amd64.exe.


Add Python 3.x to PATH Install Now:


Install Python 3 on Windows 10


Python Pip Ubuntu


Ubuntu Python 3 . . Python.


sudo apt install python3-minimal
python3 -V

Pip . ( ), Pip.


sudo apt install python3-pip
pip3 install --user --upgrade pip

Pip


Pip Windows Ubuntu.


pip help
pip search package_name
pip show package_name
pip install package_name()
pip uninstall package_name()
pip list
pip install -U()

, () --user, () .


VirtualEnv VirtualEnvWrapper


VirtualEnv Python . , , . VirtualEnv .


VirtualEnv VirtualEnvWrapper Windows


:


pip install virtualenv
pip install virtualenvwrapper-win

VirtualEnv VirtualEnvWrapper Ubuntu


Ubuntu :


pip3 install --user virtualenv virtualenvwrapper

~/.bashrc :


export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source ~/.local/bin/virtualenvwrapper.sh

, virtualenvwrapper.user_scripts creating, .


VirtualEnv


VirtualEnv Windows Ubuntu.


mkvirtualenv env-name
workon
workon env-name
deactivate
rmvirtualenv env-name

, Pip, --user:


pip3 install markdown

Windows WORKON_HOME , . , %USERPROFILE%\Envs.


PyCharm


PyCharm β€” Python. . PyCharm VirtualEnv Pip, .


PyCharm Windows


PyCharm Community Windows JetBrains. , .


. next, Install. , , .. PyCharm .


PyCharm Ubuntu


PyCharm Community Linux JetBrains. , , .


PyCharm pycharm-community, .


~/.local (Ctrl + H β€” ), opt, pycharm-community. /.local/opt/pycharm-community bin, help .. PyCharm .


:


cd /home/maksim/.local/opt/pycharm-community/bin
sh ./pycharm.sh

. desktop PyCharm. Configure β†’ Create Desktop Entry.


Creating a desktop file


PyCharm Ubuntu snap-


PyCharm snap-. Ubuntu 16.04 , PyCharm .


sudo snap install pycharm-community --classic

VirtualEnv Pip PyCharm


Pip Virtualenv PyCharm . , .


:


  1. , ;
  2. , . PyCharm , .. .

:


, matplotlib numpy, .


PyCharm Create New Project.


, Location . . 'first_program'.


, Project Interpreter. New environment using Virtualenv. . Windows venv Envs, workon PyCharm . β€” . Create.


Setting up the first program in PyCharm


, . File β†’ Settings. Project: project_name β†’ Project Interpreter.


The clean environment of the project


. : pip setuptools.


:


  • ;
  • ;
  • ;
  • .

() . . matplotlib. , Specify version Options . matplotlib . Install Package.


Install matplotlib library


, matplotlib . , numpy. .


, , first.py. :


import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 5, 100)

def gauss(sigma, mu):
    return 1/(sigma * (2*np.pi)**.5) * np.e ** (-(x-mu)**2/(2 * sigma**2))

dpi = 80
fig = plt.figure(dpi=dpi, figsize=(512 / dpi, 384 / dpi))

plt.plot(x, gauss(0.5, 1.0), 'ro-')
plt.plot(x, gauss(1.0, 0.5), 'go-')
plt.plot(x, gauss(1.5, 0.0), 'bo-')

plt.legend(['sigma = 0.5, mu = 1.0',
            'sigma = 1.0, mu = 0.5',
            'sigma = 1.5, mu = 0.0'], loc='upper left')

fig.savefig('gauss.png')

, . Add Configuration.... Run/Debug Configurations, (Add New Configuration) Python.


Name Script path Python . . Apply, OK.


Creating a configuration for a Python program


gauss.png:


Graphs of the normal Gaussian distribution


:


. , PySide2 . . , .


. .


PySide2, . , PySide2 . , PySide2.


PyCharm. . Configure β†’ Settings . Project Interpreter. , Add..., . . , β€” pyside2. Windows venv Envs, workon PyCharm . .


Creating Environment for PySide2


PySide2, matplotlib. .


PySide2. Create New Project.


, Location. , Project Interpreter, Existing interpreter pyside2.


Creating a new project using the PySide2 library


second.py :


import sys

from PySide2.QtWidgets import QApplication, QLabel
from PySide2 import QtCore

if __name__ == "__main__":
    app = QApplication(sys.argv)

    label = QLabel(QtCore.qVersion())
    label.show()

    QtCore.qVersion()

    sys.exit(app.exec_())

, . .



I do not have rich Python programming experience. And I'm not familiar with other Python IDEs. Therefore, it is possible that these IDEs also work with Pip and Virtualenv. You can use Pip and Virtualenv in the command line or in the terminal. Installing the library through Pip may fail. There are ways to install libraries without pip. You can also create virtual environments not only with Virtualenv.


In general, I just shared a small part of the experience from this area. But, if you do not go into deep jungle, then this is enough to know to write simple Python programs using third-party libraries.


All Articles