Pipenv for isolating python projects

Finding my way back to Python (6 Part Series)

1 Pyenv in Windows
2 Pipenv for isolating python projects
2 more parts…
3 Modular Flask App with Blueprints
4 Backup Your DEV.to Posts Using Python
5 Trying Poetry, another Python Dependency Manager
6 Regular Expression (Regex) with Python

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
pip install --user pipenv
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
pipenv install --three
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
- simple-flask-app
  |_ Pipfile
  |_ 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
pipenv install flask
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:

<span>from</span> <span>flask</span> <span>import</span> <span>Flask</span>
<span>app</span> <span>=</span> <span>Flask</span><span>(</span><span>__name__</span><span>)</span>
<span>@app.route</span><span>(</span><span>'</span><span>/</span><span>'</span><span>)</span>
<span>def</span> <span>hello</span><span>():</span>
<span>return</span> <span>'</span><span>Hello World!</span><span>'</span>
<span>if</span> <span>__name__</span> <span>==</span> <span>'</span><span>__main__</span><span>'</span><span>:</span>
<span>app</span><span>.</span><span>run</span><span>()</span>
<span>from</span> <span>flask</span> <span>import</span> <span>Flask</span>
<span>app</span> <span>=</span> <span>Flask</span><span>(</span><span>__name__</span><span>)</span>

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

<span>if</span> <span>__name__</span> <span>==</span> <span>'</span><span>__main__</span><span>'</span><span>:</span>
    <span>app</span><span>.</span><span>run</span><span>()</span>
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
- simple-flask-app
  |_ app.py
  |_ Pipfile
  |_ Pipfile.lock
- 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
pipenv run python app.py
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
pipenv shell
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
exit
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:

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.

Finding my way back to Python (6 Part Series)

1 Pyenv in Windows
2 Pipenv for isolating python projects
2 more parts…
3 Modular Flask App with Blueprints
4 Backup Your DEV.to Posts Using Python
5 Trying Poetry, another Python Dependency Manager
6 Regular Expression (Regex) with Python

原文链接:Pipenv for isolating python projects

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
The wise man is always a good listener.
智慧比财富更宝贵
评论 抢沙发

请登录后发表评论

    暂无评论内容