DEV Community

Dendi Handian
Dendi Handian

Posted on • Updated on

Pipenv for isolating python projects

You may want to try Poetry, which in my opinion is a little better than Pipenv. https://dev.to/dendihandian/trying-poetry-a-new-python-dependency-manager-318k. But feel free to continue here if you want to know about Pipenv.

Previously, I am able to switch between python versions. Now I want to get started to create a python project. However, if we use pip for installing a package like flask or numpy, they will be installed as global and could affect each other projects in the future. That's why we need a tool that could isolate the projects' dependencies, just like Composer in PHP and NPM in NodeJS. And the best choice so far should be pipenv. Technically, it's different from Composer and NPM because pipenv is used for creating isolated environments for each of your python projects.

Requirements

Of course, you should have python installed on your machine or alternatively follow my post about Pyenv for installing multiple python versions. With any option, make sure you could also execute pip commands.

Installing Pipenv using Pip

Open your CLI console or terminal, execute the pip command:

pip install --user pipenv
Enter fullscreen mode Exit fullscreen mode

To find the installation was successful or not, try to check the version by execute pipenv --version.

If the pipenv commands are not found just like I did in my Windows machine, then you have to add a new environment variable. To add it, press the Windows icon and start searching for edit the system environment variables and click on the found program. Click the Environment Variables button, in your user variables you should found the Path variable, Edit the variable, and start adding the path.

(CAUTION: Be careful with this, you should add, not replacing all the existing paths. I hope you know what you're doing. If not then you probably will break your machine).

In my case, my path was in C:\Users\dendi\AppData\Roaming\Python\Python38\Scripts. After adding it, then I can use the pipenv commands.

Initiating a Pipfile for your project

I assume you are using python version 3 as I am. Let's say you have a folder called simple-flask-app anywhere in your machine and then go inside the folder using CLI. Once inside the folder, execute the pipenv command:

pipenv install --three
Enter fullscreen mode Exit fullscreen mode

Then it should successfully generated the Pipfile and Pipfile.lock.

- simple-flask-app
  |_ Pipfile
  |_ Pipfile.lock
Enter fullscreen mode Exit fullscreen mode

Adding a package to your project using Pipenv

Inside the simple-flask-app folder with your CLI, let's add a package to our app and obviously we will add flask, so let's execute this as an example:

pipenv install flask
Enter fullscreen mode Exit fullscreen mode

The above command will install flask package to your (isolated) environment, as well as making changes to Pipfile and Pipfile.lock files.

Running python command with Pipenv

For the demo, let's create a simple flask app with this file named app.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

Enter fullscreen mode Exit fullscreen mode

The directory structure will be like this:

- simple-flask-app
  |_ app.py
  |_ Pipfile
  |_ Pipfile.lock
Enter fullscreen mode Exit fullscreen mode

And to run this app, execute this command:

pipenv run python app.py
Enter fullscreen mode Exit fullscreen mode

Then your app should be running and check it on your browser at http://localhost:5000

Entering the isolated environment CLI

Our project is finally isolated from our global python installation and you probably want to check something using Python shell. To enter the project's env, you can do it with this command:

pipenv shell
Enter fullscreen mode Exit fullscreen mode

Then once you are inside the environment, anything like the installed packages or maybe the configuration will differ from your global python settings. Well, do anything you want here.

And then you can simply quit the env with the command:

exit
Enter fullscreen mode Exit fullscreen mode

Pipenv Venv in Project

One thing that I recommend for you to do is by enabling the PIPENV_VENV_IN_PROJECT system variable. You can add it on windows Environment Variables:

2020-09-14-23h32-30

Having the venv folder on your project root is helpful to enable the code inspection or intellisense for python in Visual Studio Code or just making the folder is noticeable by you, just like vendor or node_modules case.

After enabling this, you may need to run pipenv install command to create the venv inside your project.


Have fun exploring Pipenv.

Oldest comments (3)

Collapse
 
elabftw profile image
eLabFTW

use pip install --user pipenv ;)

dev.to/elabftw/stop-using-sudo-pip...

Collapse
 
dendihandian profile image
Dendi Handian

Seems you're right, I'm updating the post ;) Thanks.

Collapse
 
dendihandian profile image
Dendi Handian

Forgive me, my writing is bad and my english are not good enough (still learning). I meant if we normally use pip to install pkg in global environment, they will installed globaly. I havent mentioned about virtual env or pipenv at the moment...