analitics

Pages

Showing posts with label pipenv. Show all posts
Showing posts with label pipenv. Show all posts

Wednesday, July 17, 2019

Python 3.7.3 : Using the pipenv tool.

The goal of this tutorial is how to use pipenv and manage dependencies and development environments on a collaborative Python project.
The documentation area can be found here.
C:\Python373>cd Scripts

C:\Python373\Scripts>pip install --user pipenv
Collecting pipenv
...
Successfully installed pipenv-2018.11.26 virtualenv-clone-0.5.3
If you see errors like this:
WARNING: The scripts pipenv-resolver.exe and pipenv.exe are installed in 'C:\Users\....'
which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, 
use --no-warn-script-location.
You need to add this path show into the WARNING to the windows path, see command:
setx path "%path%;C:\Users\..\Python37\Scripts"
Let use this tool to install a requests package:
C:\Python373\Scripts>cd ..

C:\Python373>cd my_flask

C:\Python373\my_flask>pipenv install requests
Creating a virtualenv for this project.
Pipfile: C:\Python373\my_flask\Pipfile
Using c:\python373\python.exe (3.7.3) to create virtualenv.
[  ==] Creating virtual environment...Already using interpreter c:\python373\pyt
hon.exe
Using base prefix 'c:\\python373'
New python executable in C:\Users\catafest\.virtualenvs\my_flask-j9hGDZgP\Script
s\python.exe
Installing setuptools, pip, wheel...
done.
Successfully created virtual environment!
Virtualenv location: C:\Users\catafest\.virtualenvs\my_flask-j9hGDZgP
Creating a Pipfile for this project.
Installing requests.
Adding requests to Pipfile's [packages].
Installation Succeeded
Pipfile.lock not found, creating.
Locking [dev-packages] dependencies.
Locking [packages] dependencies.
Success!
Updated Pipfile.lock (444a6d)!
Installing dependencies from Pipfile.lock (444a6d).
  ================================ 5/5 - 00:00:04
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
The syntax for the Pipfile is TOML, and the file is separated into sections. [dev-packages] for development-only packages, [packages] for minimally required packages, and [requires] for other requirements like a specific version of Python
If you got this error you need restarts the command prompt:
'pipenv' is not recognized as an internal or external command,
operable program or batch file.
The next command let us see output diagnostic information for use in GitHub issues:
C:\Python373\my_flask>pipenv --support | clip
C:\Python373\my_flask>pipenv --support
Check for security vulnerabilities (and PEP 508 requirements) in your environment with:
C:\Python373\my_flask>pipenv check
Checking PEP 508 requirements.
Passed!
Checking installed package safety.
All good!
Let's use the example from the documentation webpage:
import requests

response = requests.get('https://httpbin.org/ip')

print('Your IP is {0}'.format(response.json()['origin']))
The result works great.
C:\Python373\my_flask>pipenv run python my_ip.py
Your IP is ...
You can print out a tree-like structure showing your dependencies.
C:\Python373\my_flask>pipenv graph
requests==2.22.0
  - certifi [required: >=2017.4.17, installed: 2019.6.16]
...
Using this command will see a reversed tree may be more useful when you are trying to figure out conflicting sub-dependencies.
C:\Python373\my_flask>pipenv graph --reverse
certifi==2019.6.16
  - requests==2.22.0 [requires: certifi>=2017.4.17]
chardet==3.0.4
...
This command installs all the dependencies needed for development:
C:\Python373\my_flask>pipenv install --dev
Installing dependencies from Pipfile.lock (444a6d).
  ================================ 5/5 - 00:00:10
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
You can uninstall a python module:
pipenv uninstall numpy
You can replace --all with --all-dev to just remove dev packages.
Let’s start a shell in a virtual environment to isolate the development of this app:
C:\Python373\my_flask>pipenv shell
Launching subshell in virtual environment.
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

(my_flask-j9hGDZgP) C:\Python373\my_flask>
The package name, together with its version and a list of its own dependencies, can be frozen by updating the Pipfile.lock with the lock keyword.
(my_flask-j9hGDZgP) C:\Python373\my_flask>pipenv lock
Locking [dev-packages] dependencies.
Locking [packages] dependencies.
Success!
Updated Pipfile.lock (444a6d)!
Clears caches (pipenv, pip, and pip-tools) with:
pipenv --clear
Remove the virtualenv created:
(my_flask-j9hGDZgP) C:\Python373\my_flask>pipenv --rm
Close the pipenv shell:
(my_flask-j9hGDZgP) C:\Python373>exit

C:\Python373\my_flask>
This tool requires special attention in developing and programming with Python, so we have only traveled some of its real possibilities.