Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Learn How to Use the Python Virtual Environment

$
0
0

Whether you are an experienced python developer, or you are just getting started, learning how to setup a virtual environment is essential for any Python project. Join me as I cover everything you need to know about the Python virtual environment.

Make sure you read our reasons why Python programming is not useless 5 Reasons Why Python Programming Is Not Useless 5 Reasons Why Python Programming Is Not Useless Python -- You either love it or you hate it. You might even swing from one end to the other like a pendulum. Regardless, Python is a language that's hard to be ambivalent about. Read More , and if you are new to Python, check out these 10 basic Python examples 10 Basic Python Examples That Will Help You Learn Fast 10 Basic Python Examples That Will Help You Learn Fast This article of basic python examples is for those who already have some programming experience and simply want to transition to Python as quickly as possible. Read More .

What Is a Python Virtual Environment?

A Virtual Environment is a way to run different versions of Python for different projects. Similar to how virtual machines work What Is a Virtual Machine? What Is a Virtual Machine? Virtual machines allow you to run other operating systems within your current operating system, but why does that matter? What are the pros and cons? Read More , Python virtual environments allow you to install multiple versions of Python with specific modules and dependencies for each version. These projects are all independent of each other, so any modules you install in a certain project will not be accessible in other projects.

This may seem like a lot of effort, but it’s worth it. Say you normally work in Python 2.7.x but you want to try 3.x out. No problem, just create a new project and install your dependencies. What about Python 2.4.x for a legacy project? Yep, simple. None of these projects will interfere with each other, nor will they involve the version of Python used by your operating system.

Getting Set Up

It doesn’t matter what version of Python you are using. If you are using Mac then you have Python installed already. You will need to download and install Python if you are using windows.

You will need pip installed. This is a package manager for Python, and it comes with Python versions 2.7.9 or newer. All of these steps will be done through the command line, so you may want to read our guide to theWindows Command Line A Beginners Guide To The Windows Command Line A Beginners Guide To The Windows Command Line Read More or our quick guide to thelinux Command Line A Quick Guide To Get Started With The Linux Command Line A Quick Guide To Get Started With The Linux Command Line You can do lots of amazing stuff with commands in Linux and it's really not difficult to learn. Read More .

There are two packages needed to use virtual environments. Open a new terminal and install the virtualenv package:

pip install virtualenv

It is entirely possible to use and manage virtual environments with this package alone. I will not cover how to do that, as it is much easier to use the virtualenvwrapper . This is a package written to make it easy to create and manage virtual environments. Install it using pip:

pip install virtualenvwrapper

In windows you will need to install a slightly different package:

pip install virtualenvwrapper-win

Make sure you have virtualenv installed before you try to install virtualenvwrapper .

Now configure the wrapper:

export WORKON_HOME=~/Envs
source /usr/local/bin/virtualenvwrapper.sh

This wrapper stores all of your environments in the same place (instead of scattered around your filesystem, which the virtual environment will do without the wrapper).

Usage

Now that your virtual environment is all setup, you can start using it. Here’s how you create a new environment:

mkvirtualenv muo
Learn How to Use the Python Virtual Environment

This will create a folder and environment called muo inside your ~/Envs folder.

You can use this command to create as many environments as you like. It’s easy to change environments using the workon command:

workon muo

You should now see the name of your project in the command line:


Learn How to Use the Python Virtual Environment

Any packages you install will only work inside this environment.

If you no longer wish to work in an environment you need to use the deactivate command:

deactivate

It’s important to note that the workon command will deactivate the current project, and then activate the new project. There is no need to deactivate first.

It’s easy to list virtual environments:

lsvirtualenv
Learn How to Use the Python Virtual Environment

If you are usingversion control What Is Git & Why You Should Use Version Control If You’re a Developer What Is Git & Why You Should Use Version Control If You’re a Developer As web developers, a lot of the time we tend to work on local development sites then just upload everything when we’re done. This is fine when it’s just you and the changes are small,... Read More (and you really should be), make sure to exclude your environments. (Hint: Use the gitignore command if you are using Git.)

If you no longer want an environment, you can delete it:

rmvirtualenv muo

Make sure you are not currently working on that environment, otherwise you will get an error:


Learn How to Use the Python Virtual Environment

Finally, it’s easy to setup an environment with a specific version of Python:

virtualenv -p /usr/bin/python2.7 muo27

Make sure that the file path ( /usr/bin/python2.7 ) points to a version of Python (this could be any version). Notice how I have called this project muo27 . I have used the suffix 27 to indicate that this is a Python 2.7 environment.

Extras

There are a few other options you can use when creating environments. The no-site-packages option will not install packages that are already installed globally (by the operating system). These will not be accessible to your environment. This is useful for keeping a project compact and tidy, and not filling it with unnecessary packages.

You can use the freeze command to generate a list of dependencies needed for your project:

pip freeze > dependencies.txt

This will create .txt file called dependencies of all the required modules. This will make it much easier for you or another developer to get the project going again at a later date. Here’s how you can install the required modules from that list:

pip install -r dependencies.txt

Now that you know how to use Python Virtual Environments, there is no limit to the projects you can work on! Why not learn how to read and write to Google Sheets How to Read and Write to Google Sheets With Python How to Read and Write to Google Sheets With Python Python may seem strange and unusual, however it is easy to learn and use. In this article, I'll be showing you how to read and write to Google Sheets using Python. Read More and create yourself a new environment to work in.

Do you use Python Virtual Environments? What’s your favorite feature? Let use know in the comments below!

Image Credit: Sergey Nivens and Helen Dream via Shutterstock.com


Viewing all articles
Browse latest Browse all 9596

Trending Articles