DEV Community

Cover image for Create a virtual environment for Python Projects
Elechi George
Elechi George

Posted on

Create a virtual environment for Python Projects

Hello there, in this short note I'll show you how to create a python virtual environment for a new project and walk you through the process of resolving some common conflicts you might get while creating a virtual environment for your projects.

First things first, you will need to make sure python and pip are installed properly

to check this just run the stuff below in your favourite terminal

  $ python --version
Enter fullscreen mode Exit fullscreen mode

if you an error like the one below, it probably because you don't have python correctly installed or you it haven't be added to your system environment variables path

Image showing how to setup environment variable on windows

Here's how to add it yourself

type "edit system environment variable" - follow the images below to get it resolved

Once you type that stuff from before, it should land you here, look to the bottom where it says environment variable CLICK IT !

Image showing how to setup environment variable on windows

That button should land you here, DOUBLE CLICK where that blue color stuff is on [path] just like the image below

Image showing how to setup environment variable on windows

That double clicking should land you here, make sure yours is like mine in the picture below [especially the first two]

Image showing how to setup environment variable on windows

Okay great, once you finish editing "click save" and go back to this area, and DOUBLE CLICK where my cursor is on the image below that has a blue background

Image showing how to setup environment variable on windows

It should land you here, make sure yours looks like mine, especially the last two variables

Image showing how to setup environment variable on windows

when you are done, click save until the boxes disappear, you shouldn't be having error with python and pipenv

MOVING FARWARD and Installing Pipenv

Pipenv manages dependencies on a per-project basis. To install packages, change into your project’s directory (or just an empty directory) and run:

  $ cd code_folder

  $ pip install pipenv --user 

  $ pipenv install requests
Enter fullscreen mode Exit fullscreen mode

it does it what it does in creating a virtual environment for your project and out out some that looks like the image below...

Image success on creating a virtual environment

You should be all set, if you want to use virtualenv instead of pipenv - all you need to do is run the commands below:

  $ pip install virtualenv --user
Enter fullscreen mode Exit fullscreen mode

then to use it, run:

  $ cd project_folder
  $ virtualenv venv
Enter fullscreen mode Exit fullscreen mode

to activate it run:

  $ source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

In summary to setup a virtual environment

  • install python

  • install pipenv

  • change the environment variables to point accordingly

  • then run pipenv install requests on the folder you want to create your python project

CHEERS, If you have questions, let me know in the comments

Thanks

Top comments (1)

Collapse
 
shriekdj profile image
Shrikant Dhayje

wow how well you documented each and every step.