create account

Setting Up Python Virtualenv by tomshwom

View this thread on: hive.blogpeakd.comecency.com
· @tomshwom ·
$4.19
Setting Up Python Virtualenv
# Preface
---

This is loosely a part 2 to my **[Getting Started with Bash on Windows](https://steemit.com/technology/@tomshwom/getting-started-with-bash-on-windows)** post.  If you already have a working Python development environment, you can skip that post and continue here.

<center>![](https://steemitimages.com/DQmWnk4zGe2g48okpHubji56bJPy8WsSmwtAovGyZi7ZKDY/image.png)</center>

# What is a Virtual Environment?
---

Virtual environments are similar to [virtual machines](https://en.wikipedia.org/wiki/Virtual_machine), 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](https://steemit.com/technology/@tomshwom/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:

```virtualenv --version```

<center>![](https://steemitimages.com/DQmeDxshaRoRURS4cM3LXio45S9noBSE7b1o7Y4J4Zx6TUB/image.png)</center>

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:

```virtualenv my_app```

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:

```source activate```

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.

```vim```

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()
```

Hit the ```esc``` key, and type:

```:wq hello.py```

This will (w)rite, (q)uit, and name the file "hello.py".

Now we can run our Flask program by typing:

```python hello.py```

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!

<center>![](https://steemitimages.com/DQmeuzgpa2TWdDkwjU6axRWP96NCszGA91jk5KRtdngW2aS/image.png)</center>

Hit ```Ctrl + C``` to shut down the Flask app when you're done.

### Closing the virtualenv

When you're done using your virtualevn, simply use:

```deactivate```

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!

<center>**[Python for Beginners - How to use Python virtualenv](http://www.pythonforbeginners.com/basics/how-to-use-python-virtualenv) ••• [Flask Quickstart guide](http://flask.pocoo.org/docs/0.10/quickstart/)**</center>

---

<center><sub>Leave a **like** or **resteem** if you found this helpful. You can also support me by sending ETH or ERC20 tokens to **Tomshwom.eth**. Leave your thoughts below about what you think and on which content you'd like to see in the future!</sub></center>
👍  , , , , , , ,
properties (23)
authortomshwom
permlinksetting-up-python-virtualenv
categoryprogramming
json_metadata{"tags":["programming","python","developer","virtualenv"],"image":["https://steemitimages.com/DQmWnk4zGe2g48okpHubji56bJPy8WsSmwtAovGyZi7ZKDY/image.png","https://steemitimages.com/DQmeDxshaRoRURS4cM3LXio45S9noBSE7b1o7Y4J4Zx6TUB/image.png","https://steemitimages.com/DQmeuzgpa2TWdDkwjU6axRWP96NCszGA91jk5KRtdngW2aS/image.png"],"links":["https://steemit.com/technology/@tomshwom/getting-started-with-bash-on-windows","https://en.wikipedia.org/wiki/Virtual_machine","http://www.pythonforbeginners.com/basics/how-to-use-python-virtualenv","http://flask.pocoo.org/docs/0.10/quickstart/"],"app":"steemit/0.1","format":"markdown"}
created2017-10-26 21:52:36
last_update2017-10-26 21:52:36
depth0
children5
last_payout2017-11-02 21:52:36
cashout_time1969-12-31 23:59:59
total_payout_value3.228 HBD
curator_payout_value0.960 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,108
author_reputation1,713,446,395,686
root_title"Setting Up Python Virtualenv"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id18,644,248
net_rshares1,905,290,101,235
author_curate_reward""
vote details (8)
@naveendavisv ·
another option is installing Anaconda. Below video explains the details.
<iframe width="560" height="315" src="https://www.youtube.com/embed/7rYPHZ1FVQE" frameborder="0" allowfullscreen></iframe>
properties (22)
authornaveendavisv
permlinkre-tomshwom-setting-up-python-virtualenv-20171027t035314958z
categoryprogramming
json_metadata{"tags":["programming"],"app":"busy/1.0.0"}
created2017-10-27 03:53:21
last_update2017-10-27 03:53:21
depth1
children0
last_payout2017-11-03 03:53:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length195
author_reputation32,557,718,122
root_title"Setting Up Python Virtualenv"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id18,661,588
net_rshares0
@not-a-bird ·
$0.02
>  virtual environments focus on a single application space for programs to run isolated from others

You make it sound like it's a container solution that provides process isolation.  Doesn't it just provide you with an environment with all the dependencies you need to run your program?
👍  
properties (23)
authornot-a-bird
permlinkre-tomshwom-setting-up-python-virtualenv-20171031t125433492z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-10-31 12:54:33
last_update2017-10-31 12:54:33
depth1
children2
last_payout2017-11-07 12:54:33
cashout_time1969-12-31 23:59:59
total_payout_value0.017 HBD
curator_payout_value0.006 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length288
author_reputation4,828,771,219,503
root_title"Setting Up Python Virtualenv"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id19,068,965
net_rshares12,115,001,541
author_curate_reward""
vote details (1)
@tomshwom ·
That is correct.  It's not process isolation, just package/environment isolation.
👍  
properties (23)
authortomshwom
permlinkre-not-a-bird-re-tomshwom-setting-up-python-virtualenv-20171031t133019635z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-10-31 13:30:18
last_update2017-10-31 13:30:18
depth2
children1
last_payout2017-11-07 13:30:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length81
author_reputation1,713,446,395,686
root_title"Setting Up Python Virtualenv"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id19,072,305
net_rshares1,829,817,775
author_curate_reward""
vote details (1)
@not-a-bird ·
$0.02
Thanks for clearing that up.  I wasn't sure if I had missed some kind of update that added container support.
👍  
properties (23)
authornot-a-bird
permlinkre-tomshwom-re-not-a-bird-re-tomshwom-setting-up-python-virtualenv-20171102t000453514z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-11-02 00:05:03
last_update2017-11-02 00:05:03
depth3
children0
last_payout2017-11-09 00:05:03
cashout_time1969-12-31 23:59:59
total_payout_value0.017 HBD
curator_payout_value0.005 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length109
author_reputation4,828,771,219,503
root_title"Setting Up Python Virtualenv"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id19,213,651
net_rshares12,115,618,226
author_curate_reward""
vote details (1)
@randowhale ·
$0.02
This post received a 5.0% upvote from @randowhale thanks to @tomshwom!  For more information, [click here](https://steemit.com/steemit/@randowhale/introducing-randowhale-will-you-get-the-100-vote-give-it-a-shot)!
👍  
properties (23)
authorrandowhale
permlinkre-setting-up-python-virtualenv-20171029t195622
categoryprogramming
json_metadata"{"app": "randowhale/0.1", "format": "markdown"}"
created2017-10-29 19:56:24
last_update2017-10-29 19:56:24
depth1
children0
last_payout2017-11-05 19:56:24
cashout_time1969-12-31 23:59:59
total_payout_value0.024 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length212
author_reputation47,657,457,485,459
root_title"Setting Up Python Virtualenv"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id18,909,027
net_rshares12,114,919,312
author_curate_reward""
vote details (1)