[Django] Setting up a virtual environment

1 minute read

Why create a virtual environment?

It separates the modules that will be used for a project from the files on the computer. For example, for a specific project, you might need an older version of Django. Also, if virtual environment is not used, it might be hard to keep track of all the installed modules.

For Windows:

1. Create a project directory

>> mkdir myproject
>> cd myproject

2. Create a virtual environment

If virtualenv is not installed, it should be installed as follows.

>> pip install virtualenv

It is also a good idea to keep pip updated using

>> python -m pip install --upgrade pip

Now, inside the project folder, create a virtual environment.

>> python -m venv myvenv

Here, -m is to import module, venv is the virtual environment module, and myvenv is an arbitrary name.

3. Activate the virtual environment

>> myvenv\Scripts\activate

To deactivate, simply enter

>> deactivate

4. Install modules

Install the necessary modules, in this case the latest version of Django.

>> (myvenv) pip install django

and use pip freeze to check the installed modules in the virtual environment.

>> (myvenv) pip freeze

5. Connecting virtual environment in PyCharm

In File - Settings - Python Interpreter - Add - Existing Interpreter - Select Scripts/python.exe

Updated:

Leave a comment