This is loosely a part 2 to my Getting Started with Bash on Windows post. If you already have a working Python development environment, you can skip that post and continue here.
What is a Virtual Environment?
Virtual environments are similar to virtual machines, but rather than emulating an entire system, virtual environments focus on a single application space for programs to run isolated from others.
Virtualenv will let you run multiple versions and instances of Python at once, without letting them interfere with eachother.
Setting Up Virtualenv
If you followed my Getting Started with Bash on Windows post, you're ready to open up your terminal and get started.
Getting virtualenv installed
First, we want to check and see if we already have virtualenv installed. Do this by typing:
If you didn't get a similar output to what's above, you probably don't have virtualenv installed. Install it using one of these commands:
sudo apt-get install python-virtualenv <--One I'd recommend for noobies
sudo easy_install virtualenv
sudo pip install virtualenv
Once it's installed, check to make sure by using the
virtualenv --version command.
Prepare your virtual environment
Navigate to the directory where you want the virtual environment to be stored, and create a new folder to contain everything by using the
mkdir command (or simply create a new folder in the file explorer). Name it something appropriate like "virtualenvironment_python_3.6".
Navigate inside this folder and create your virtual environment by running:
If you want to isolate the site packages, add
--no-site-packages to the end of this command.
After a short wait, you can now navigate inside your "my_app" directory to the "bin" folder and activate the virtual environment by using:
You will see that your shell prompt now says "(my_app)" at the beginning, indicating that you are inside your virtualenv.
Any packages you install using pip or easy_install will now be installed to the "my_app/lib/pythonX.X/site-packages" directory, completely isolated from other virtualenvs and your main system.
Virtualenv does not create a new Python environment, it uses links to existing files. This means you need an active Python environment already installed on your system.
Using your virtualenv
Lets run through a small example to demonstrate things.
To install a package to your virtualenv, use pip install X. No need to sudo the command since the files are being stored in the directory created by your user account. For example:
pip install flask
Now we can create a Python program using the Flask web framework.
To keep things in the terminal, lets run VIM and write a short program.
This should open VIM where you'll be greeted by the VIM welcome message.
i to inter insert mode.
If you remember the tips from my previous post, you can copy & paste what's below into your terminal next.
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
esc key, and type:
This will (w)rite, (q)uit, and name the file "hello.py".
Now we can run our Flask program by typing:
It will inform you that the Flask app is running and on which address/port. You can now navigate to your web browser and enter this info to see your web app!
Ctrl + C to shut down the Flask app when you're done.
Closing the virtualenv
When you're done using your virtualevn, simply use:
Now you know how to install and use virtualenv for your Python projects! I relied heavily on the following resources for writing this short tutorial, and if you encounter any issues they should be your first reference. If you can't find what you're looking for here, feel free to leave a comment below and somebody will try to help!