Python Package Management
Pip, PyPi, Pipenv, Virtualenv, virtual environments, basic usage of pipenv.
18 flashcards · Programming Languages · by lakesare
How to install all dependencies from your project's requirements.txt?
pip install -r requirements.txt
What's meant by python module vs package?
module: simply a Python source file, which can expose classes, functions and global variables. package: a directory of Python module(s) that contains an additional __init__.py file. mypackage/__init__.py <-- this is what tells Python to treat this directory as a package mypackage/mymodule.py ___So then you would do: from mypackage.mymodule import myclass Packages can be nested to any depth, provided that the corresponding directories contain their own __init__.py file.
Can package contain modules? Other packages?
Yes, yes. But when we import a package, only its immediate modules are imported, not subpackages.
This is how you import in Java. Name corresponding imports in Python. fully qualified name. import package.hi; - only hi will be accessible import package.*; - all the classes and interfaces of this package will be accessible but not subpackages.
import package; print(package.hi); from package import hi; print(hi); from package import *; print(hi); In general they are very similar, except that in Python before using a fully qualified name you got to still import it.
How to turn simple requirements into exact requirements? Simple requirements.txt (A list of top-level dependencies a a project has, often without versions specified): requests[security] flask gunicorn==19.4.5 Exact requirements.txt (A complete list of all dependencies a project has, each with exact package versions specified): cffi==1.5.2 cryptography==1.2.2 enum34==1.1.2 Flask==0.10.1 gunicorn==19.4.5 idna==2.0 ipaddress==1.0.16 itsdangerous==0.24 Jinja2==2.8 MarkupSafe==0.23 ndg-httpsclient==…
This type of requirements.txt is generated from the output of running $ pip freeze from within a current working runtime environment for the application.
Package management using pip is usually done either via simple or exact requirements, explain.
Simple requirements.txt (A list of top-level dependencies a project has, often without versions specified): requests[security] flask gunicorn==19.4.5 Exact requirements.txt (A complete list of all dependencies a project has, each with exact package versions specified): cffi==1.5.2 cryptography==1.2.2 enum34==1.1.2 Flask==0.10.1 gunicorn==19.4.5 idna==2.0 ipaddress==1.0.16 itsdangerous==0.24 Jinja2==2.8 MarkupSafe==0.23 ndg-httpsclient==0.4.0 pyasn1==0.1.9 pycparser==2.14 pyOpenSSL==0.15.1 reque…
What's a virtual environment in Python?
A virtual environment is a tool to maintain separate space for a project with its dependencies and libraries in one place. This environment is specific to the particular project and doesn't interfere with other projects' dependencies. For example, you can work on project X which is using version 1.0 of library Z and also maintain project Y which is using version 2.0 of library Z.
Name 3 ways to import and use m.py: ### EXAMPLE PYTHON MODULE # Define some variables: numberone = 1 ageofqueen = 78 # define some functions def printhello(): print "hello" def timesfour(input): print input * 4 # define a class class Piano: def __init__(self): self.type = raw_input("What type of piano? ") self.height = raw_input("What height (in feet)? ") self.price = raw_input("How much did it cost? ") self.age = raw_input("How old is it (in years)? ") def printdetails(self): print "This piano…
(Assumes that the module is in the same directory) import m print m.ageofqueen from m import ageofqueen print ageofqueen from m import * print(ageofqueen) (But don't use wildcard imports, it's hard to know what's in our scope now) ___ In contrast, in Java: fully qualified name. import package.hi; - only hi will be accessible import package.*; - all the classes and interfaces of this package will be accessible but not subpackages.
What does PyPI stand for?
Python Package Index
What is PyPI?
Official third-party software repository for Python. Some package managers, including pip, use PyPI as the default source for packages and their dependencies.
What is the officially recommended packaging tool for python?
Pipenv. While pip alone is often sufficient for personal use, Pipenv is recommended for collaborative projects as it’s a higher-level tool that simplifies dependency management for common use cases. Proof: https://packaging.python.org/tutorials/managing-dependencies/#managing-dependencies
What was a standard dependency management workflow before pipenv?
virtualenv + pip: when developing, you would create a Python virtual environment using virtualenv in order to isolate your project from the system Python or other virtual environments. pip install ... all the packages you need pip freeze Issue with approach this is: Uninstalling with pip will not remove dependencies for a package! We need two separate files for dev. test and production packages Links: https://bryson3gps.wordpress.com/2017/11/08/stop-everything-start-using-pipenv
Ruby: bundler Js: npm Python: ?
pipenv Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world.
What files does pipenv use instead of requirements.txt?
Pipfile - abstract dependency declarations Pipfile.lock - last tested combination
How to use pipenv?
pipenv install --three - to initialize pipenv for python 3 graph will show you a dependency graph, of your installed dependencies. shell will spawn a shell with the virtualenv activated. run will run a given command from the virtualenv, with any arguments forwarded (e.g. $pipenv run python or $ pipenv run pip freeze). check checks for security vulnerabilities and asserts that PEP 508 requirements are being met by the current environment.
Should you gitignore Pipfile? Pipfile.lock?
No, keep both Pipfile and Pipfile.lock in version control. Generally, keep both Pipfile and Pipfile.lock in version control.
How to run something in a pipenv project?
pipenv run ...
pipenv shell VS pipenv run ...?
Pipenv shell Pipenv shell is used mainly as a run it now, and leave. It will create an active environemnt almost the same way as with source env/bin/activate, but it will also do it within a new shell. That way you cannot use previous commands from history. There is even a bug about that but the conclusion is that it's not pipenv issue, rather pew problem with sub-shell. As it's using a sub-shell, it may not be advised to use it within your deployment/management. For example if you want to make…