No more struggles Windows Python development! I’ve found this is the best way to configure your dev environment.
This has made things much easier to get started and less of a headache overall.
We use Virtual Environment so we can test python code in encapsulated environments and to also avoid filling our base Python installation with a bunch of libraries we might use for only one project.
But Virtual Environments can be tricky if you don’t establish a good workflow. I’ll show you how to setup your python environment from Scratch and then do a very simple workflow using Flask.
First Go to the Python Downloads Site.
As of March 2015 the download you want for a standard windows machine is Windows x86-64 MSI installer (The other download is for servers). Its circled here:
Run the installer!
You’ll come across this page in the installer:
You’ll want to scroll down and add it to the path. If you don’t that’s okay. You can add it later.
Adding Python to the PATH will allow you to call if from the command line.
After the installation is complete double check to make sure you see python in your PATH. You can find your path by opening your control panel -> System and Security -> System -> Advanced System Settings -> Environment Variables -> Selecting Path -> Edit ->
Now you’re looking at your Path. Be Careful, if you delete or add to the path accidently you may break other programs.
You need to confirm that C:\Python27; and C:\Python27\Scripts; is part of your path.
If you do not see it in your path you can simply add it at the beginning or end of the variable value box. As you can see in the image below.
As of Python Version 2.7.9 Pip is installed automatically and will be available in your Scripts folder.
If you install a later version of Python I would recommend installing it according to this helpful stackoverflow post.
Pip is a Package manager for python which we will use to load in modules/libraries into our environments.
An example of one of these libraries is VirtualEnv which will help us keep our environments clean from other Libraries. This sounds really confusing but as you start using it you’ll begin to understand how valuable this encapsulation of modules/libraries can be.
To test that Pip is installed open a command prompt (win+r->’cmd’->Enter) and try ‘pip help’
You should see a list of available commands including install, which we’ll use for the next part:
Now that you have pip installed and a command prompt open installing virtualenv to our root Python installation is as easy as typing ‘pip install virtualenv’
Now we have virtualenv installed which will make it possible to create individual environments to test our code in. But managing all these environments can become cumbersome. So we’ll pip install another helpful package…
This is the kit and caboodle of this guide.
Just as before we’ll use pip to install virtualenvwrapper-win. ‘pip install virtualenvwrapper-win’
Excellent! Now we have everything we need to start building software using python! Now I’ll show you how buttery smooth it is to use these awesome tools!
Make a Virtual Environment
Connect our project with our Environment
Set Project Directory
Make a Virtual Environemt:
Lets call it HelloWold. All we do in a command prompt is enter ‘mkvirtualenv HelloWold’
This will create a folder with python.exe, pip, and setuptools all ready to go in its own little environment. It will also activate the Virtual Environment which is indicated with the (HelloWold) on the left side of the prompt.
Anything we install now will be specific to this project. And available to the projects we connect to this environment.
Connect our project with our Environment:
Now we want our code to use this environment to install packages and run/test code.
First lets create a directory with the same name as our virtual environment in our preferred development folder. In this case mine is ‘dev’
HelloWold will be the root folder of our first project!
Set Project Directory:
Now to bind our virtualenv with our current working directory we simply enter ‘setprojectdir .’
Now next time we activate this environment we will automatically move into this directory!
Let say you’re content with the work you’ve contributed to this project and you want to move onto something else in the command line. Simply type ‘deactivate’ to deactivate your environment.
Notice how the parenthesis disappear.
You don’t have to deactivate your environment. Closing your command prompt will deactivate it for you. As long as the parenthesis are not there you will not be affecting your environment. But you will be able to impact your root python installation.
Now you’ve got some work to do. Open up the command prompt and type ‘workon HelloWold’ to activate the environment and move into your root project folder.
Pretty sweet! Lets get working.
To use flask we need to install the packages and to do that we can use pip to install it into our HelloWold virtual environment.
Make sure (HelloWold) is to the left of your prompt and enter ‘pip install flask’
This will bring in all the tools required to write your first web server!
Now that you have flask installed in your virtual environment you can start coding!
Open up your favorite text editor and create a new file called hello.py and save it in your HelloWold directory.
I’ve simply taken the sample code from Flask’s website to create a very basic ‘Hello World!’ server.
I’ve named the file hello.py.
Once the code is in place I can start the server using ‘python hello.py’ this will run the python instance from your virtual environment that has flask.
You can now navigate with your browser to http://127.0.0.1:5000/ and see your new site!
Sweet. You have everything you need to start working through tutorials on Flask without worrying about gunking up your Python installations.
Let me know if you have any questions! Happy Developing!
70 thoughts on “Python, Pip, virtualenv installation on Windows”
Thanks. Works like a charm! This is one of the reasons why I kept switching to Linux as it was seamless there but, windows most often poses problems while installing. Now, I think I can configure a final dev environment in windows for python development.
I have Python 3.5.2 installed on my machine. I want to setup virtual environment and run Python 2.7 associated code in it. Can I do that ? How ?
You can install both Python 2.7 and Python 3.5 on your machine then when building your virtual environment specify which python version you want to use. See this blog post for how to do it. See this blog post for how to do it
Thank you very much for your straight forward steps. Finally up and running after searching answers from lots of websites
Hello, I’m trying to follow this guide, but when I execute this(I’m using PowerShell on win7):
pip install virtualenv-win
It gave me this error:
Could not find a version that satisfies the requiment virtualenv-win (from versions: )
No matching distribution found for virtualenv-win
What should I do?
Thanks in advance.
I would try stepping through this tutorial one more time to make sure you followed the instructions precisely.
You’ll need python and and python/scripts on your path.
Awesome! . Concise Instructions! Clear explanation!!
Great. It worked prefectly. Thanks a lot.
Awesome worked like charm! I was searching for this since last 2 days. Thank you so much!
!!!! The most helpful post among the most helpful posts that I’ve read until now. Thank you very much…You saved me hours(or days).
Thank you for your excellent article.
But I am having a real problem with Windows 10 and file permissions.
The persistent error I am getting is:
PermissionError: [Errno 13] Permission denied: ‘c:\\program files\\python36-32\\Lib\\site-packages\\virtualenv.py’
and I cannot see any way around this, can you assist me?
My first foray into creating a virtual environment has not been without its challenges.
Did you install python to the root directory?
Do you have admin privileges on the computer you’re developing on?
I have the same error when trying to open a python file in IDLE. Windows 10, Python 3.6 (I also have 2.7 installed and recently removed 3.2 to make some space on my computer) I am very new to this and have had no luck finding a solution online. It says:
[Errno 13] Permission Denied: C:\\Users\\ cb\\.idlerc\\recent-files.lst
Any suggestions/solutions would be greatly appreciated.
Im in 2017 and this really helped me a lot as a newbie! Great article!
Very clear description. I am certain I would never had got Flask running on windows without this.
starting from the begining to the flask’s website its ok, but i have failed to get the details in the last command prompt you showed
A very clear and precise article.
Which tutorial or document is recommended to learn Flask?
I have bookmarked this page and it has always been a life saver. Thanks
Thanks! Nice work.
It was very helpful.
Very Helpful and helped me set up and use the virtual environment the first time!! Clear, simple and precise instructions. Thanks
I need a little more help. I created the virtual environment but it did not seem to activate nor did the SAD prompt appear on the command line.
PS C:\Users\sad0804> mkvirtualenv SAD
C:\Users\sad0804\Envs is not a directory, creating
Using base prefix ‘c:\\users\\sad0804\\appdata\\local\\programs\\python\\python36-32’
New python executable in C:\Users\sad0804\Envs\SAD\Scripts\python.exe
Installing setuptools, pip, wheel…done.
Also, the setprojectdir . command results in:
ERROR: A virtualenv must be activated.
Pass in a full or relative path to the project directory.
If the directory doesn’t exist, it will be created:
v.good and straightforward tutorial
If you ever need to delete a virtual environment, use
I have checked your website and i’ve found some duplicate content,
that’s why you don’t rank high in google, but there is a tool that can help you to create 100% unique articles, search
for: Boorfe’s tips unlimited content
Gooooood point… I didn’t think of that. Any examples to demonstrate proper implementation?
Thanks for the tutorial !!!
Awesome article that really helped me a lot in setting up the env.
adding to it – just in case we want to choose a different directory other than C drive ,we can do so while
Make a Virtual Environemt:
we need to browse to that particular directory
ex : D:
and than we can run the mkvirtualenv HelloWold
Thanks so much for this tutorial. My goal is to develop a very basic (HTML only) webpage, place it on a Raspberry PI3, on my LAN then access this remotely whereby data will be entered via text field on the webpage and stored in an Sqlite3 database (again on the RP3).
Does this sound plausible to you?
I was just following the instructions for setting up Python virtualenvs and found that the command for setting the project directory is wrong in this article; I’m guessing it changed recently. As per this StackOverflow post (https://stackoverflow.com/questions/36215890/why-setprojectdir-command-is-not-found-in-virtualenvwrapper-while-other-command), the command is now just “setvirutalenvproject” without any arguments, you just run that in the directory you want to be the dev environment.
Thanks for this tutorial, super useful!
Hey Men, really Thanks so much for help the community. I passed a lot of time with out a solution but you could help me very simply.
Timmy Reilly, your explanation are crystal clear. Thank you for your guidance.
extremely helpful guide, thanks.
one addendum: activating virtualenvs within powershell seems to be problematic, the simplest fix is to run cmd first and then mkvirtualenv etc.
You saved me from a headache. I was using git bash and was unable to run mkvirtualenv until I saw your comment. Thank you!
I’ve come to adore Conda on Windows, but still have to deal with a lot of issues installing packages not found on Conda. This walkthrough for creating virtualenvs in vanilla Python was perfect and easy to follow. Much appreciated!!!
This is perfect!
please i am requesting for a full tutorial of flask
See Miguel Grinberg’s blog and books!
Good article. Keep me updated.
Incredibly clear. Superior to all the python books’ treatment of this subject, including python.org documentation.
IF I install python 3.7.x but for some project i need to use python 3.5 then how can I change the current version? Does virtualenv supports such feature ? If yes how to do it ?
See my other blog here! http://timmyreilly.azurewebsites.net/running-multiple-python-versions-on-windows/
I tried following this way, got one doubt in between. You have used cd dev. here dev represents which folder ?
can you please specify
That is a directory that he already had created in his path.
So, if you want to copy that path structure you just need to mkdir dev & cd dev & mkdir HelloWold & cd HelloWold
Thanks. This worked for me, for both django and Flask.
Fantastic guide, well done!
Thanks but how use it with gitbash? Im getting:
bash: workon: command not found
With all commands and when i tried install again, i got:
$ pip install virtualenvwrapper-win
Requirement already satisfied: virtualenvwrapper-win in c:\users\diego\appdata\local\programs\python\python37\lib\site-packages\virtualenvwrapper_win-1.2.5-py3.7.egg (1.2.5)
Requirement already satisfied: virtualenv in c:\users\diego\appdata\local\programs\python\python37\lib\site-packages (from virtualenvwrapper-win) (16.1.0)
I had the same problem on Windows using gitBash from the HelloWorld director. However, when I ran the pip virtualenv install into my path through command prompt as admin it worked. Then I setup my git repo in my HelloWorld dir. I hope this may help others who had the same challenge.
sorry this is all what mine got, any problem to solve this please , need an answer…
Command “”c:\program files\python37\python.exe” -u -c “import setuptools, tokenize;__file__=’C:\\Users\\OLUWAT~1\\AppData\\Local\\Temp\\pip-install-dev4ujul\\virtualenvwrapper-win\\setup.py’;f=getattr(tokenize, ‘open’, open)(__file__);code=f.read().replace(‘\r\n’, ‘\n’);f.close();exec(compile(code, __file__, ‘exec’))” install –record C:\Users\OLUWAT~1\AppData\Local\Temp\pip-record-u9dc2e5t\install-record.txt –single-version-externally-managed –compile” failed with error code 1 in C:\Users\OLUWAT~1\AppData\Local\Temp\pip-install-dev4ujul\virtualenvwrapper-win\
You are using pip version 18.1, however version 19.0.3 is available.
You should consider upgrading via the ‘python -m pip install –upgrade pip’ command.
thank you so much; it helped me to try the following successfully
Deploying a Flask Application to AWS Elastic Beanstalk:
Thank you for writing this very helpful tutorial on Flask. It has helped me in my workflow setup so much since I am completely new to python as well as virtual environments.
I used mkvirtualenv in Visual Studio Code. It worked, sort of as there is now a directory but there is no reference to it in the prompt. TIA
Are you using CMD or powershell in the VS Code terminal?
I have installed virtualenv and virtualenvwrapper-win
but still ‘mkvirtualenv’ is still not recognized as an internal or or external command.
Please help me…
Are you using CMD? Have you tried opening the command prompt again after running the installation?
Thanks for the tutorial, just what I needed!
Launching subshell in virtual environment…
‘powershell.exe’ is not recognized as an internal or external command,
operable program or batch file.
I cant activate the virtual environment that i created…..please help
Once I open browser per the link provided above, http://127.0.0.1:5000/ , what should I expect to see? I only see the followng page error:
The connection was reset
The connection to the server was reset while the page was loading.
The site could be temporarily unavailable or too busy. Try again in a few moments.
If you are unable to load any pages, check your computer’s network connection.
If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.
If I go into browser and enter http://127.0.0.1:5000/ I get the same result. If I open the file from browser, I merely see the saved in the hello.py file. I assume this means I am not ready to see the result of the code. Can someone please assist?
hmmm, is there any output in your terminal?
I follow all instruction from the beginning to the end, all steps are successful until the end when entering the last command, python hello.py nothing as output appeared from my terminal…
Even when entering the local host URL error message appeared as: This site can’t be reached 127.0.0.1 refused to connect.
If you restart your terminal. Powershell or cmd does it work? Is python in your ‘PATH’?
Thank you very much, it’s save my day.
I cannot start virtual env using the command ‘mkvirtualenv venv’ even after installing it by ‘pip install virtualenv’. Can someone help me please?