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
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.
Of course, you should have python installed on your machine and it's recommended to
Pyenv for installing multiple python versions. With any option, make sure you could also execute
Open your CLI console or terminal, execute the pip command:
pip install --user pipenv
To find the installation was successful or not, try to check the version by execute
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.
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
Then it should successfully generated the
- simple-flask-app |_ Pipfile |_ Pipfile.lock
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
The above command will install flask package to your (isolated) environment, as well as making changes to
For the demo, let's create a simple flask app with this file named
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World!' if __name__ == '__main__': app.run()
The directory structure will be like this:
- simple-flask-app |_ app.py |_ Pipfile |_ Pipfile.lock
And to run this app, execute this command:
pipenv run python app.py
Then your app should be running and check it on your browser at http://localhost:5000
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:
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:
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:
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
After enabling this, you may need to run
pipenv install command to create the venv inside your project.
Have fun exploring Pipenv.