This tutorial will cover setting up a Python Environment from scratch on a Windows Machine.
You are welcome to use either Python 2 or Python 3, but for the sake of this tutorial we are using Python 2.7.
Download Python 2.7 for Windows from the official Python website here. Once the download is done, click on the package and follow along with the install wizard. If you are given a choice for where to install Python, chose C:\Python27
Open the command line
Search your apps for the application “cmd”. This is the command line tool.
Update your Environment Variables
Environment variables tell your system where to find things on your machine, including software that you have installed. We are going to create an environment variable for Python’s directories so we can access it quickly and easily. This will enable us to run python by just typing “python”, instead of having to type “C:\Python27\python” all the time.
In the lower box titled System Variables, click “New” to create a new system variable. Name it “PYTHON_HOME” and give it the value that corresponds to where YOUR python install is. Click “OK” to save.
Next, we’re going to edit an existing system variable. Find the system variable named “Path” and click “Edit”. Add this text to the END of your Path variable’s value field:
and click “OK” to save your change.
Type ctrl+c to close the python interpreter.
Pip is a package manager. A package manager is a tool that is used to keep your third-party libraries managed in a simple and orderly way on your machine.
Go to https://bootstrap.pypa.io/get-pip.py to download the script get-pip.py. The website should prompt you to save the file. Save it to your Downloads directory.
Close the command line and re-open it.
If you see a list of contents or nothing at all, that’s good! That means that you successfully ran the pip command. If you see a bunch of errors, that’s bad.
FYI: The command we just ran, pip freeze is how you ask pip to show you all of the packages that you have installed. That’s why it’s ok if you saw nothing when you ran the command. That just means you haven’t installed any packages yet.
Congrats! We now have the ability to easily install third-party packages.
Even though we won’t do it today, it is common to work on many python projects at the same time. You might need to install a lot of packages for one project, but only need a few packages for another. Even worse, you might need one version of a package for one project, and a different version for another.
This could potentially be a real headache. Take this example:
You have project CatsBlogs which is using the package Django version 1.5. You have another project, DogsBlog, which is on Django 1.8. Every time you want to work on CatsBlog, you have to downgrade Django to 1.5… but then if you want to work on DogsBlog, you have to upgrade it back to 1.8! Ugghhhh.
Fortunately, we have a solution to this problem, and that solution is virtual environments. A virtual environment will keep all of your packages for each project isolated from each other, allowing you to install multiple versions of multiple packages on your computer at the same time, without them colliding with each other.
Let’s install our virtual environment software, and we’ll go through an example.
Notice how my command line now has (myenv) in front of the C:\Users\michelle>. This is the command line telling me that I am now inside of my “myenv” virtual environment.
Also note that your output told you where your new python executable is. Mine looks like this:
This path is very important and we will need it later. So take a mental note of where it is.
Now that we have a virtual environment, let’s look around a bit to help us get comfortable with how it works. While we’re here inside the virtual environment, let’s install another package with pip. We’ll install the package tweepy.
Cool. Also notice that I have more packages than you probably do. That’s ok. You only need to have tweepy to be successful at this point.
Notice that the “(myenv)” has disappeared from your command line. This means you are no longer inside of your virtual environment.
Now type “pip freeze”. Notice that tweepy is gone! This is because we are now outside of the virtual environment. Tweepy is installed only inside our virtual environment.
Type “pip freeze” again from inside of the virtual environment and see that tweepy is back again!
Congrats again! We now have the ability to keep our packages contained and safe for each project we make.
Setting up PyCharm
PyCharm is an Integrated Development Environment, which is used to write python code.
If you don’t have PyCharm yet, download the free Community edition here.
Our first order of business is to get PyCharm set up to use our new virtual environment, so that we can use the packages that we install with pip.
Open up PyCharm and create a new project.
Remember earlier when I told you to remember where the python executable for your virtual environment is? Mine was:
Select your Python from inside your virtual environment and click “OK”.
Now, let’s test and make sure that we can access our python packages from inside of PyCharm! Remember that the package that we installed was called tweepy. Let’s try to import it in PyCharm.
Just to test that our package is available to us, type “import tweepy”. If PyCharm underlines your import statement with red squiggles, that means that PyCharm cannot find the package, and your setup was not successful. If PyCharm greys out your import statement like in the screenshot below, that means that PyCharm has successfully found your package and is now just waiting to use it!
Congrats! You can now access you packages from inside of PyCharm.