A shell script to make sure you don't forget the pipenv shell again

Introduction

* pipenv, are you using it? *

pipenv is a tool that easily and automatically manages packages for the project and builds a virtual environment. It's a very convenient one that also works with pyenv.

But do you have such an experience?

When I ran it, an error occurred, so I put it in with pip, but it just didn't enter the virtual environment ...

I do a lot.

Therefore!

I wrote a shell script that pipenv shell when cd where the Pipfile is

Had made

~/.bash_autopipenv


#!/bin/bash

function ispipenv()
{
if [ "$PIPENV_ACTIVE" == 1 ]; then
  :
else
  if [ -e "Pipfile" ]; then
      pipenv shell
  fi
fi
}

function pipenv_cd()
{
  \cd $@ && ispipenv
}

alias cd='pipenv_cd'

The points are the following parts.

if [ "$PIPENV_ACTIVE" == 1 ]; then
   ...

When you enter the virtual environment, you are using the environment variable PIPENV_ACTIVE to be defined. Since PIPENV_ACTIVE is not defined if you are not in the virtual environment, it is evaluated with "$ PIPENV_ACTIVE" to avoid errors. Now the pipenv shell will not run when you are in the virtual environment.

Also, if \ cd in pipenv_cd is changed to cd, Note that pipenv_cd is called in pipenv_cd and pipenv_cd is called in it.

Always load

If you add the following to .bash_profile or .bashrc, it will always be read, so you will not forget to run pipenv shell: ok_hand:

if [ -f ~/.bash_autopipenv ]; then
  . ~/.bash_autopipenv
fi

Summary

--Fun to create your own bash with shellscript: innocent: ――Next, don't forget to exit!

Recommended Posts

A shell script to make sure you don't forget the pipenv shell again
I'll never forget how to write a shell script, don't forget! !!
Conditional branch due to the existence of a shell script file
Create a shell script to run the python file multiple times
Write a script in Shell and Python to notify you in Slack when the process is finished
[Ubuntu] How to execute a shell script
Creating a shell script to write a diary
When you make a mistake in the directory where you execute `pipenv install`
Would you like to make a Twitter resume?
Don't forget to close the file just because it's in a temporary folder
Don't ask "Are you sure you want to continue connecting"
[Python] Make sure the received function is a user-defined function
Make a note of what you want to do in the future with Raspberry Pi
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
A Python script that allows you to check the status of the server from your browser
How to run a Python program from within a shell script
[Linux] Copy data from Linux to Windows with a shell script
You who color the log to make it easier to see
A shell script that just emails the SQL execution result
Put the lists together in pandas to make a DataFrame
Process the files in the folder in order with a shell script
I wrote a script to combine the divided ts files
Django Make choices only for the facility you belong to
I will publish a shell script created to reduce the trouble of creating LiveUSB on Linux
Let's try a shell script
Process the contents of the file in order with a shell script
[Blender] Script to check if the selected one is a mesh
To make sure that the specified key is in the specified bucket in Boto 3
How to make a command to read the configuration file with pyramid
I wrote a script to get you started with AtCoder fast!
Don't you want to say that you made a face recognition program?
A script that returns 0, 1 attached to the first Python prime number
How to determine if a shell script was started in bash
How to find if you don't know the Java installation directory