Pipenv And Pew
- Overview
- Installing
pipenvandpew - Getting Started With Environments
- Using Environments
- Miscellaneous Tips and Tricks
Overview
This goes over the basics of how I use pipenv and pew to manage Python environments for my projects/applications. I’ve collated most of this information from:
pipenv is used for the bulk of environment creation/management here and pew is used primarily for activating Python environments from anywhere in the system. pipenv also has the added bonus of Visual Studio Code integration as well! Visual Studio Code automatically detects environments created with pipenv and allows you to use them from within Visual Studio Code’s integrated terminal.
Installing pipenv and pew
To install pipenv and pew, it’s as simple as going through your current pip installation.
- Execute the following in a terminal:
pip install pipenv pip install pew - Follow the instructions here to get
pewto display the virtual environment name in the terminal prompt.- Tip: If you don’t know which shell type you’re using, it’s probably bash if using a Linux machine and it’s powershell if you’re using a Windows machine.
Getting Started With Environments
This section covers what you need to know to get up and running with pipenv environments.
Creating A New Environment
In a directory that does not have a Pipfile in it, execute the following to create an environment for the project in the directory:
pipenv shell
This launches a new shell session that:
- Creates a Pipfile
- Creates the associated environment and environment directory in your
~/.local/share/virtualenvs/directory.- Note: The environment is name
<the current directory name>-<hash>where<hash>is a unique hash to the current directory. This is to avoid name conflicts if you have multiple environments with the same name directory. This would occur if you had multiple clones of the same repository, for example.
- Note: The environment is name
Creating An Environment From An Existing Pipfile
pipenv is really great for creating environments from Pipfiles. To do this:
- Navigate to the directory containing the Pipfile for the environment you’re wanting to use/create.
- Execute:
pipenv install
Using Environments
This section covers how to actually use environments.
General Workflow
The general workflow for using an environment is to:
- Create the environment, either from scratch or based off an existing environment.
- Activate the environment by following these instructions.
- Do whatever you would like to do with Python.
- Deactivate the environment by exiting the shell session.
Activating An Environment
You can activate environments in two seperate ways:
- By using
pipenvin the project directory that contains the Pipfile. - By using
pewanywhere in your system.
Activating An Environment Using pipenv
To activate an environment using pipenv:
- Navigate to the directory of your project/application containing the Pipfile.
- Kick up a new shell session of your environment by typing:
pipenv shell
Activating An Environment Using pew
To activate an environment from anywhere in your system, use pew in the following way:
- List all of your virtual environments with:
pew workonor
pew ls - Activate the environment that you wish to work on with:
pew workon <environment name> -nBe sure to include the full name (with the unique hash at the end of the human-readable name) to activate the environment in this way. The
-nflag is sopewdoesn’t change your current directory to the directory the project associated with the environment is in.
Exiting An Environment Session
Since pipenv and pew start a new shell session with the virtualenv pathing instead of changing the pathing in the current shell, you have to actually exit the shell by using the exit command or by typing Ctrl-d to deactivate the environment.
Executing Python Files
There are several ways that you can execute Python files using pipenv environments.
From Within The Environment
You can execute whatever Python file that you’re wanting to execute from within your environment doing the following:
- Kick up a shell session with your active environment by following these instructions.
- Execute your python file normally (using
python <my file name>.py).
From Outside The Environment
You can execute a Python file outside of your environment by doing the following:
- Navigate to the directory containing the Pipfile of the environment you’re wanting to use.
- Execute the instruction set for what you’re wanting to do with
pipenv runprepended to the command.- For example, if you’re wanting to print “Hello, World!” out using your custom environment, you’d navigate to the directory containing the Pipfile for that custom environment and do the following:
pipenv run python -c "print('Hello, World!)" - Note: you can do some pretty cool stuff with custom script shortcuts using
pipenv.
- For example, if you’re wanting to print “Hello, World!” out using your custom environment, you’d navigate to the directory containing the Pipfile for that custom environment and do the following:
Developing With Environments
When developing a project or application that uses pipenv, you’ll want to be sure to git add and git commit your Pipfile anytime that you add or update packages using pipenv install <package name>.
Installing New Packages
When developing Python applications, it is inevitable that you’ll need to install a package to help you along the way, like Jupyter Notebooks, NumPy, SciPy, OpenCV, or even the Python Trello API interface. When this becomes necessary:
- Search for the package using the Python Package Index.
- Navigate to the directory containing the Pipfile of the environment you are wishing to install the package to.
- Execute the following to install the package:
pipenv install <package name>pipenvautomatically installs the package and resolves any dependency issues for you (through thepipbackend) and also updates your Pipfile and Pipfile.lock files for you! Be sure togit addandgit committhe changes to the Pipfile and Pipfile.lock files once you’re sure that you’ve downloaded the correct package.
Miscellaneous Tips and Tricks
Scripting With pipenv
https://pipenv-fork.readthedocs.io/en/latest/advanced.html#custom-script-shortcuts
I’d use this to, for example, avoid having to type:
pipenv run python myTrello.py daily
and could instead run:
pipenv run daily
by modifying my Pipfile.
Using pew with pipenv
Pipenv is a great way to set up environments and export them as determinate environments, locking the environment in a state that is known to work. However, I kind of hate that you can only activate the environment from the directory that contains the Pipfile. To prevent this, you can install pew using your pip installation.
I love that in Anaconda you can activate a Python environment from anywhere and use that environment for anything that you need. I wanted the same functionality out of pipenv.
- Revisiting this, I’m not quite sure if this is best practice sort of behavior. Either way, I’m keeping this up here for my own reference.
Listing Environments
pew workon
This lists all of the environments available to you.
Activating an Environment
pew workon <environment name>
This activates the environment from anywhere in your system. You can then run your pipenv run commands (you can learn how to create these in the “scripting” section) from anywhere in the system.