Setting up a virtual environment in Python
In this post, we are going to set up a virtual environment in Python and as an example install Django 1.8.18 (LTS).
Although you can set up a virtual environment in any distribution of Python, in this post we are going to use WinPython, a portable version of python that you can run from a USB drive on any Windows machine. No installation or special permissions are required!
If you already have a Python distribution installed that you want to use, you can skip to step 2 and just install virtualenv.
This is what it looked like on my machine.
For this example, create a directory called virtenv and cd into it. The virtual environment will be created in this folder. Working in WinPython Command Prompt.exe type
Choose a name for your virtual environment, for the sake of simplicity (or lack of imagination), call it project1. Create the environment by typing
The folder virtenv now contains a sub folder called project1. This is the folder for the virtual environment in which we will install and experiment with Django 1.8.18 (LTS). To start using this virtual environment you have to activate it. Type
Note how the command line starts with (project1). This is to indicate that you are running python in a virtual environment.
As a reference, this is a screen capture from my computer
Installing packages in this virtual environment is trivial, for example, to install Django 1.8.18 (LTS) type
To check what packages are installed in your environment, run pip freeze
Although you can set up a virtual environment in any distribution of Python, in this post we are going to use WinPython, a portable version of python that you can run from a USB drive on any Windows machine. No installation or special permissions are required!
If you already have a Python distribution installed that you want to use, you can skip to step 2 and just install virtualenv.
1. Install WinPython
Download your choice of WinPython from sourceforge.net. Here I chose WinPython-64bit-3.6.2.0Qt5.exe. Run the exe file and you will have a folder as shown in the image below. During the installation, or rather unpacking of the files, you must choose a destination folder. Here I chose to unpack the files to a folder called WinPython-64bit-3.6.2.0Qt5 that is located in the root of my drive, in my case d:\WinPython-64bit-3.6.2.0Qt5\. Although the folder name is long, it has the advantage to keep track on what version of Python you are running, but you can choose a shorter name, like d:\Python.
Contents of the destination folder after running WinPython-64bit-3.6.2.0Qt5.exe
2. Install virtualenv
If you installed WinPython as described above, then start WinPython Command Prompt.exe and install virtualenv by using pip,
D:\WinPython-64bit-3.6.2.0Qt5\Scripts>pip install virtualenv
This is what it looked like on my machine.
3. Create a virtual environment and install Django
You can have several different virtual environments with different packages installed on your machine. Each virtual environment is located in a folder. To get somewhat organized, I collect all my virtual environments in a top-level folder called virtenv, located in the root of drive D. But you can choose whatever name you like.For this example, create a directory called virtenv and cd into it. The virtual environment will be created in this folder. Working in WinPython Command Prompt.exe type
D:\WinPython-64bit-3.6.2.0Qt5\Scripts>mkdir d:\virtenv
D:\WinPython-64bit-3.6.2.0Qt5\Scripts>cd d:\virtenv
d:\virtenv>
D:\WinPython-64bit-3.6.2.0Qt5\Scripts>cd d:\virtenv
d:\virtenv>
Choose a name for your virtual environment, for the sake of simplicity (or lack of imagination), call it project1. Create the environment by typing
d:\virtenv>virtualenv project1
The folder virtenv now contains a sub folder called project1. This is the folder for the virtual environment in which we will install and experiment with Django 1.8.18 (LTS). To start using this virtual environment you have to activate it. Type
d:\virtenv>project1\Scripts\activate
(project1) d:\virtenv>
(project1) d:\virtenv>
Note how the command line starts with (project1). This is to indicate that you are running python in a virtual environment.
As a reference, this is a screen capture from my computer
Installing packages in this virtual environment is trivial, for example, to install Django 1.8.18 (LTS) type
(project1) d:\virtenv>pip install Django==1.8.18
To check what packages are installed in your environment, run pip freeze
(project1) d:\virtenv>pip freeze
And that's it! You have a virtual environment called project1 and in this environment, we have installed Django 1.8.18 LTS. A reference screen from my computer is shown below.
If you would like to see an example of how to set up a Django project then read section 3.
4. Set up a Django project in a virtual environment
Setting up a Django project using a virtual environment is straightforward. The first decision to make is, where do you want to store the code that you develop? For this example, we are going to create a Django project called trydjango18, and we are going to store this project inside the virtual environment project1, this might not be the best practice, but it serves as an example.Working in WinPython Command Prompt.exe type
(project1) d:\virtenv>cd project1
(project1) d:\virtenv\project1>django-admin startproject trydjango18
(project1) d:\virtenv\project1>django-admin startproject trydjango18
It is worth nothing that the command on windows is django-admin startproject trydjango18 and not django-admin.py startproject trydjango18 as written in some web sources.
After running this command you will have a Django project folder called trydjango18 . If you followed my set up, you will have this folder hierarchy
virtenv/
project1/
Include/
Lib/
Scripts/
tcl/
trydjango18/
trydjango18/
__init__.py
settings.py
urls.py
wsgi.py
manage.py
pip-selfcheck.json
Note that you have two folders named trydjango18, an outer folder and an inner folder. This can be confusing, and it is ok to change the name of the outer folder. For convenience, change the name of the outer trydjango18 folder to src. In this folder we will develop the Django project.
After the name change, your folder hierarchy should look like this
virtenv/
project1/
Include/
Lib/
Scripts/
tcl/
src/
trydjango18/
__init__.py
settings.py
urls.py
wsgi.py
manage.py
pip-selfcheck.json
You can check that Django is working by the first cd into the src folder and then starting the development server
(project1) d:\virtenv\project1\src>python manage.py runserver
If everything worked you should have a confirmation that the development server started. Visit http://127.0.0.1:8000/ and make sure you have the greeting page.
5. Conclusions & Summary
In this post, we have installed a portable version of Python for windows, WinPython, with the virtual environment package. To create and work in a virtual environment follow these steps:- Start WinPython Command Prompt.exe
- Make a directory for your virtual environment and cd into this directory
- Create the virtual environment by typing virtualenv name
- Activate the virtual environment by typing: name/Scrits/activate
- Develop your code....
- End the virtual environment by typing: deacativate
Suspension & Steering Repair Specialists
ReplyDeleteGreat! Thanks so much!!!
ReplyDeleteBest Car exhaust and mufflers specialists in Perth
ReplyDeleteReally helpful right down to the ground; pleased to read such a helpful piece. I learned a lot from it, and I'll definitely remember it. Continue to spread the word custom erp development
ReplyDelete