Home » Categories » Linux Cloud Server » Python |
Common Python Tools: Using virtualenv, Installing with Pip, and Managing |
Article Number: 267 | Rating: Unrated | Last Updated: Sat, Jan 4, 2014 at 2:18 AM
|
When it comes to working with Python, especially in the domain of application development, there are certain tools that you will see being mentioned often in various places or open source code. Despite being extremely commonly used, unfortunately sometimes it is hard to get a hold of a good manual to walk you through each step, which is absolutely vital when it comes to getting familiar with such important and needed tools. In this DigitalOcean article, we aim to fill you in on not only the basics, but also the logic behind popular Python tools and items as we dive into using them in real life scenarios. We will begin with downloading and installing some common libraries, setting and working with virtual environments (using virtualenv), and managing packages for development and production of your own applications.
Python on CentOSPlease remember that if you are using a CentOS/RHEL system, you should abstain from working with the default Python interpreter that is shipped with the operating system. Instead, you should opt for installing Python yourself. Likewise, in order to install pip and virtualenv on CentOS with a custom Python installation, you can follow the instructions on that article. Python and PackagesAlthough Python applications can be made of a single file, usually they consist of a series of functions, objects (classes), handy tools and of course, variables spread across multiple file(s), placed inside modules. These modules together make up what is referred as a package. The traditional way of installing a package involves first spotting it and then downloading. It sounds soft and simple because it actuallyislike many things in Python -but it is not perfect. When the files are ready and unpacked, using the disutils module, you can install it by calling
In spite of the simplicity of the procedure explained above, it is no use if the challenge abstracted from installing exists elsewhere in the process: finding and managing them. This is where package management via tools comes in - bringing along several benefits such as:
Package ManagementPackages in Python can be tools, libraries, frameworks and applications. Given the popularity and the beauty of the language, there are tens of thousands of packages available which you can make use of for your own projects. Package Management ToolsThe two most common Python package managers are pip and easy_install. Both of them aim to help users with the tasks of:
Both of them might appear to do the same thing from the outside and their joint dependence on the common library setuptools increases this notion. However, in this case, it is what's hidden from the eye that makes the difference — and a lot of it as well. pip vs easy_installThe first tool created for the task was easy_install. Although it was a relief and a pleasure to use compared to doing everything manually, over time it has proven to be problematic in certain aspects. That created the grounds for development of pip, another package manager. pip (as defined by the project itself) is a replacement for easy_install, which brings many benefits over its predecessor, including, but not limited to:
A Thorough pip How-ToIn this section, we will talk about getting the necessary dependencies for pip, installing its latest built followed by a walk-through of the core functionality offered such as installing, uninstalling, freezing and managing requirements. When would I use pip? As promised at the introduction, we aim to give you examples of real life scenarios. Imagine that you are undertaking the development of a small application. You have set yourself a roadmap, and everything is going well. Then you discover a library (or a module) that can be of great help to you if you included in your application. You can download it the traditional way as we have explained. However, once you have not just one but 3, 4 or even 20 packages you need to deal with, this process becomes cumbersome. Include managing them (e.g. updating, uninstalling, replacing, using a different version), you can see the problems you will need to deal with, which are made redundant using pip, the package manager. Installing pipIn order to install pip, we first need to take care of its dependencies. Do not worry though, it is very easy. setuptools As explained above, one of the dependencies of pip is the setuptools library. It builds on the (standard) functionality of Python's distribution utilities toolset called distutils. Given that distils is provided by default, all we need left is setuptools. We are going to securely download the setup files for setuptools using These setup files, which Python interpreter is going to execute, automate the installation process as they set up the latest stable version on our system.
This installation gives us the ability to use pip globally across the system. However, this is not the preferred way to install any other package. What is recommended is to always use self-contained Python environments, virtualenv. We will talk about it in the next section. Note: You might need to explicitly gain super user privileges in order to continue with the download. In that case, consider using:
After having its single dependency installed, we can now continue with downloading and setting uppip. We will be again using
In order to use it without stating the full path, it must be appended to PATH.
After completing this step, we are ready to work with pip. Using pipUsing pip is really fun and can be considered headache free. If you have dealt with extremely unnecessary problems in the past and did not even understand why, pip will ensure that they are kept to a minimum for you hereon. Installing packages using pippip can do many things but it would not be a mistake to state that the most often used function of it is installing packages. There are several ways it can handle this job for you.
Uninstalling packages with pip:The second most common function of pip is probably uninstalling packages.
Upgrading packages with pipIf you are thinking of uninstalling to install a newer version of an application, you can try upgrading.
Searching for packages with pipBefore deciding to remove or upgrade a package, you might feel the need to first search for one.
Creating a list of installed packages with pipOne of the most truly exceptional and life saving abilities of pip is being able to create - with ease - lists ("freeze") of packages installed. This is also often called requirements. Depending on your Python environment (whether it be a virtual or a global one), pip will create a file listing all the packages installed with one single command.
Note: This command will output a file in the current working directory.
Note: This command will output a file in the current working directory.
Installing all the packages from a list with *pipWhen you are working on an application - preferably inside a virtual environment - you will have all of its dependencies (required packages) installed there. After having extracted a list of them using freeze, you can get them installed again with install.
A Thorough virtualenv How-ToLet's begin with defining what exactly virtualenv is and the situation where it comes in handy. virtualenv: In the world of Python, an environment is a folder (directory) which contains everything that a Python project (application) needs in order to run in an organised, isolated fashion. When it is initiated, it automatically comes with its own Python interpreter - a copy of the one used to create it - alongside its very own pip. There are a number of problems that virtualenv solves:
Using virtualenv is the recommended way for working with Python projects, regardless of how many you might be busy with. It is very easy to use and an excellent tool to have at your disposable. It truly does wonders when coupled with pip. We will start with installing virtualenv the system. Installing virtualenvIn order to install virtualenv, we are going to call in pip for help. We will install it as a globally available package for the Python interpreter to run. There are two ways to obtain the application. The version you will be able to get depends on which one you choose. The simplest method is using pip to search, download and install. This might not provide you the latest stable version.
The latest release of virtualenv is
UsingvirtualenvUsing this tool consists of getting it to create a folder, containing the Python interpreter and a copy of pip. Afterwards, in order to work with it, we need to either specify the location of that interpreter or All the applications you install using the interpreter inside the virtual environment will be places within that location. When you use pip to create a list of them, only the ones inside the folder will be compiled into a file.
Creating / Initiating a virtual environment (virtualenv)
Activating avirtual environment
Working with a virtual environment without activatingFor various reasons, you might choose not to activate the environment before using it. This brings more flexibility to commands you run, however, you need to make sure to target the correct interpreter each and every time.
Using thepipinstallation inside the environment without activation
Deactivating a virtual environment:
|
Attachments
There are no attachments for this article.
|
How To Set Up Python 2.7.6 and 3.3.3 on CentOS 6.4
Viewed 7437 times since Sat, Jan 4, 2014
How To Create Nagios Plugins With Python On Ubuntu 12.10
Viewed 6704 times since Sat, Jan 4, 2014
A Comparison of Web Servers for Python Based Web Applications
Viewed 2670 times since Sat, Jan 4, 2014
How To Create Nagios Plugins With Python On CentOS 6
Viewed 3627 times since Sat, Jan 4, 2014
How To Setup uWSGI On Ubuntu 12.10
Viewed 2156 times since Sat, Jan 4, 2014
How to Deploy Python WSGI Applications Using uWSGI Web Server with Nginx
Viewed 6773 times since Sat, Jan 4, 2014
How To Use the Web2py Framework to Quickly Build Your Python App
Viewed 4578 times since Sat, Jan 4, 2014
How To Use the Pyramid Framework To Build Your Python Web App on Ubuntu
Viewed 4113 times since Sat, Jan 4, 2014
How To Work with the ZeroMQ Messaging Library
Viewed 8194 times since Sat, Jan 4, 2014
Docker Explained: How To Containerize Python Web Applications
Viewed 4614 times since Sat, Jan 4, 2014
|