Slider Revolution Plugin For WordPress
October 22, 2021Studies And Online Courses To Better Yourself
November 30, 2021When doing development work using Python you will often find yourself using packages that you install using pip. Pip is similar to npm for Javascript, gem for Ruby, and NuGet for .NET, it is the standard package manager for Python. It allows the user to manage additional dependencies and libraries that aren’t part of the standard Python library. Pip has become so integral to the Python ecosystem that you no longer have to install it separately, and it is bundled with the Python installer. To see if you have pip installed on your machine you can type pip --version
. If you get an error it is highly advisable that you update your Python version, as opposed to installing pip separately.
To install packages using pip you simply type pip install [packageName]
into your command line and pip will install your package for you. This package will now be available in any of your Python projects as long as you import it. Unlike npm which installs its dependencies locally pip will install its packages globally by default. When you have multiple projects that you’re working on, or collaborative projects it is better to keep the packages within their projects and not all global on your system. This is where and why we use virtual environments.
The virtual environment is a package that is installed through pip using the command pip install virtualenv
. Pip will install the virtualenv package, which you will then use to create local environments for each of your projects. The local environments mean that you limit the number of times you hear things like ‘but it worked on my machine’. Once we have virtualenv installed we must create a virtual environment for our project. We’ll setup a new project from scratch to illustrate this.
Firstly, we’ll create a new folder on our filesystem to house the new project. Once we have our project folder, we need to create our virtual environment within the project folder. To do this we type virtualenv [environmentName]
. The standard is to just call your virtual environment “venv”. Once virtualenv has setup our virtual environment we need to activate it. This will differ depending on your operating system. In Powershell you would activate the venv by typing .\[environmentName]\Scripts\activate.ps1
, in cmd you would replace “.ps1” with “.bat”. In Linux you activate the environment by typing source ./venv/bin/activate
. To leave your virtual environment you simply type deactivate
.
In my next blog post I’ll talk about git, security, and dotenv files. Check out https://pypi.org/ to have a look at all the packages available for installation through pip.
1 Comment
I am 100% sure that I am preaching to the converted, but have you tried pipenv (pip install pipenv) it’s a great way to setup and use different virtual environments. I used it once and now I am spoiled, there is also “Poetry”, however it seems less stable…