Your First Guide to Getting Started with “pipenv”

What is “pipenv”?

There are many package manager tools in other programming languages such as:

  • Ruby
    • bundler
  • PHP
    • composer
  • NodeJS
    • npm
    • yarn
  • Rust
    • cargo

If you know any of the above tools, it might be easy to understand what it is.
pipenv is just a package management tool for Python as same as those tools.

Why “pipenv”?

The official documentation Pipenv: Python Dev Workflow for Humans and this blog post Why Python devs should use Pipenv would tell us why we should use pipenv.

Two main problems of “before” pipenv workflow are following:

  • Using pip and virtualenv separately is not necessary any more!
  • requirements.txt is problematic: see A Better Pip Workflow™
    • without version specified -> different versions every time for dev/prod environments!
    • with version specified -> --upgrade is hard!

If we look back on the history of Python package management tools, there are many tools before, and this is terrible.

  • Package manager
    • Easy Install
    • pip
  • Many many virtual environment tools
    • virtualenv
    • venv
    • pyenv
    • pyenv-virtualenv
    • etc

Finally, pipenv comes to our rescue!

Setup pipenv

Check Python version (should be 3.x)

$ python --version
Python 3.6.0

Enter fullscreen mode Exit fullscreen mode

Check pip version

$ pip --version
pip 18.1

Enter fullscreen mode Exit fullscreen mode

Install pipenv

brew install pipenv

Enter fullscreen mode Exit fullscreen mode

Check pipenv version

$ pipenv --version
pipenv, version 2018.11.26

Enter fullscreen mode Exit fullscreen mode

Success: pipenv is installed!

pipenv workflow

Install dependencies (pipenv automatically detect requirements.txtand solve the dependencies)

$ cd [YOUR PROJECT]
$ pipenv install

Enter fullscreen mode Exit fullscreen mode

Pipfile and Pipfile.lock should be created

  • Pipfile: list of all installed packages

(Pipfile example)

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
jupyter = "*"
seaborn = "*"
numpy = "*"
pandas = "*"
plotly = "*"
scipy = "*"
six = "*"
sklearn = "*"
pyspark = "*"
flake8 = "*"
autopep8 = "*"
Flask = "*"

[requires]
python_version = "3.7"

Enter fullscreen mode Exit fullscreen mode

  • Pipfile.lock: maintain a proper dependencies and sub packages with version

(Pipfile.lock example)

{ "_meta": { "hash": { "sha256": "xxxxxxxxxxxxxxxxxxxxxxxx" }, "pipfile-spec": 6, "requires": { "python_version": "3.7" }, "sources": [ { "name": "pypi", "url": "https://pypi.org/simple", "verify_ssl": true } ] }, "default": { "appnope": { "hashes": [ "sha256: xxxxxxxxxxxxxxxxxxxxxxxx", "sha256: xxxxxxxxxxxxxxxxxxxxxxxx" ], "markers": "sys_platform == 'darwin'", "version": "==0.1.0" }, "attrs": { "hashes": [ "sha256: xxxxxxxxxxxxxxxxxxxxxxxx", "sha256: xxxxxxxxxxxxxxxxxxxxxxxx" ], "version": "==19.1.0" }, "autopep8": { "hashes": [ "sha256: xxxxxxxxxxxxxxxxxxxxxxxx" ], "index": "pypi", "version": "==1.4.4" ... 

Enter fullscreen mode Exit fullscreen mode

Now, run your python script (Installed packages are available when you use pipenv run)

$ pipenv run python [YOUR PYTHON MAIN SCRIPT]
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
 Success! 
Updated Pipfile.lock (980232)!
Installing dependencies from Pipfile.lock (980232)…
     ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 73/73 — 00:01:22
To activate this project's virtualenv, run pipenv shell. Alternatively, run a command inside the virtualenv with pipenv run. 

Enter fullscreen mode Exit fullscreen mode

Thanks for reading!
If you enjoyed this article, feel free to hit that clap button .

References

原文链接:Your First Guide to Getting Started with “pipenv”

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容