[Python Tips] Are you still using Pip?

avatar

In a previous tip I talked about Virtual Environments and why you should be using them. While there are a few ways to do them, I want to talk more in-depth about my favorite.

Introducing pipenv

Pipenv is like a combination of Pip, VirtualEnv, and NPM although technically it's a combination of Pip & VirtualEnv.

PipEnv brings package.json type of functionality with the file Pipfile.

Pip makes it super easy to create virtual environments, switching to virtual environments, and running under a virtual environment without switching. It also manages your dependencies and gives you an easy way to look at your dependency graph.

Before you can use pipenv, you need to install it.

Pipenv installation

Pipenv is available on PyPi, so all you have to do is:

pip install -U pipenv

I recommend keeping pipenv global, so installing it directly is ideal.


Using pipenv

You can use pipenv similar to how you would use NPM to install packages and update the package file (in this case Pipfile).

One of the biggest features that most people don't even know about (I'm guessing they don't) is the additional security pipenv brings. Pipenv uses hashes everywhere and checks the file hashes for all locked dependencies.

Unlike VirtualEnv, pipenv does not create copies of modules and packages in your working folder.

Beem installed w/ VirtualEnv

Beem installed w/ Pipenv

Pipenv allows you to easily specify Python 2 or Python 3 when creating an environment.

Pipenv has two main commands for running your applications.

shell - To enter your environment
run - To run code without entering environment first

I'm not going to get into creating a Pipfile manually, Pipenv does a good job managing it automatically. There are some cases you might want to edit it by hand but that's beyond the scope of this tutorial.

Creating a virtual environment with pipenv

I'm going to skip this, because all you need to do is install a module and pipenv will start creating an environment for you.


Installing modules with Pipenv

Just like pip, all you do is

pipenv install <module>

If there is not a Pipfile, it will create one and start a new environment.

You can uninstall packages using pipenv uninstall.

You can use a requirements.txt file to load your dependencies similar to pip by using pipenv install -r requirements.txt.

You can then use pip install to install all packages from the Pipfile when you are ready to deploy are installing on a new machine.


Switching to your environment

Once you installed your packages, you can switch to your new virtual environment by using:

pipenv shell

This will enter the virtual environment, much like you do with VirtualEnv.

You can skip that all together and just run pipenv run python mycode.py arguments to just execute your code without switching first.


Updating Modules

You can easily upgrade modules by running pipenv update, pipenv will make the necessary changes to the Pipfile and Pipfile.lock for you.


Dev Packages

Like NPM, and unlike pip, you can install packages for development only. These are packages you need to do development (like linting software) but for the final deployment, they are not needed.

All you do is add --dev to the install command.

pipenv install --dev pylint

You can install all packages including dev packages using pip install --dev.


Dependency Graph

Pipenv has a really cool feature to look over the dependency graph of your application.

Just type pip graph and you will see something like this:

hivepy==0.9.999
  - appdirs [required: Any, installed: 1.4.4]
  - certifi [required: Any, installed: 2020.4.5.2]
  - ecdsa [required: >=0.13, installed: 0.15]
    - six [required: >=1.9.0, installed: 1.15.0]
  - funcy [required: Any, installed: 1.14]
  - future [required: Any, installed: 0.18.2]
  - langdetect [required: Any, installed: 1.0.8]
    - six [required: Any, installed: 1.15.0]
  - prettytable [required: Any, installed: 0.7.2]
  - pycrypto [required: >=1.9.1, installed: 2.6.1]
  - pylibscrypt [required: >=1.6.1, installed: 1.8.0]
  - scrypt [required: >=0.8.0, installed: 0.8.15]
  - toolz [required: Any, installed: 0.10.0]
  - ujson [required: Any, installed: 3.0.0]
  - urllib3 [required: Any, installed: 1.25.9]
  - voluptuous [required: Any, installed: 0.11.7]
  - w3lib [required: Any, installed: 1.22.0]
    - six [required: >=1.4.1, installed: 1.15.0]
PyYAML==5.3.1
requests==2.23.0
  - certifi [required: >=2017.4.17, installed: 2020.4.5.2]
  - chardet [required: >=3.0.2,<4, installed: 3.0.4]
  - idna [required: >=2.5,<3, installed: 2.9]
  - urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.9]

Really handy if you want an overview of your dependencies.


There are more features I suggest you check out if you have more questions. A good resource for pipenv can be found here.

If you are using Git, it is recommended to check in both Pipfile & Pipfile.lock.

As you can see, Pip & VirtualEnv are no match for Pipenv.

I have written a lot of Python tutorials, check them out!


My Python Tips Series


Securely chat with me on Keybase

Why you should vote me as witness



0
0
0.000
9 comments
avatar
(Edited)

Thank you for sharing this way of making virtual environments (pipenv). Very well explained and now I will practice it. The two ways you knew are:
a) virtualenv python-exercise
b) python -m venv python-exercise

Every day I learn more about python, this time thanks to you @themarkymark

0
0
0.000
avatar

I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!

0
0
0.000
avatar

Hey @themarkymark I hope you are doing great :)
I would like to ask you if you could point me to the latest tutorial for the KeyChain, because I found 2 yesterday and I am not sure if there is another one, more recent.
Sorry to put you to trouble ...
Thanks in advance!

0
0
0.000
avatar

I don't have an up to date tutorial, but it is really easy.

Look in the extension store for Hive Keychain, make sure it is the one from @yabapmatt and install it.

Once installed, it will prompt you to set a password, you will need this to unlock the extension. From there you can add your account(s) and it will manage authenticating to Hive services for you.

I do recommend setting the idle lock-in the preferences to lock the extension when you are not using it. It tends to lock even when you are using it though after a set time.

If you still have questions, let me know and I can make a newer tutorial.

0
0
0.000
avatar

Thanks so much, this is extra helpful and I feel really silly for not checking on github first before asking you :)
I appreciate your time!

0
0
0.000
avatar

You don't want to use GitHub, it is now on the Chrome Web Store (for Chrome and Brave) and on the Firefox store for Firefox, I recommend going directly there.

0
0
0.000
avatar

I've been using Pipenv for several months, but there was a long hiatus in their releases, leading many to think the project was dead.

Releases have recently resumed. In the meantime, it seems that poetry might have surpassed Pipenv in features and design.

@themarkymark, have you tried https://python-poetry.org/? I'm looking forward to trying it for my next Python project.

0
0
0.000
avatar

No I haven't but I think I heard of it then forgot to actually try it. I will check it out but I really love pipenv. I love how npm works and pipenv gives very similar functionality but better in some ways (no 3500 module node_modules folder).

Python has always been more clunky with virtual environments.

0
0
0.000
avatar

Pipenv is great. At my day job we've started using pip-compile: https://github.com/jazzband/pip-tools and it's been working really well for us.

Coming from the PHP community we have fantastic tools like https://getcomposer.org which is similar to what you'd find with NPM with a file you define dependencies (package.json, composer.json) and another file locking the versions to what has been installed (package-lock.json, composer.lock). It seemed silly to me that in Python dependency management is still seeing a lot of tooling changes and iteration but it seems headed in the right direction.

0
0
0.000