cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1383
Views
5
Helpful
2
Comments
Dirk-Jan Uittenbogaard
Cisco Employee
Cisco Employee

242px-Python-logo-notext.svgTeam,

As I'm using a lot of virtual environments with PIPENV, it became harder to keep track of the environments that I have setup. And that was the reason to create this script which:

  1. List all of your (pipenv) virtual environments
  2. Select one
  3. Change to the actual folder

Requirements

  • MacOS
  • use of Pipenv (instead of the default virtualenv)

 

Script:

#!/bin/zsh
my_env_folder="$HOME/.local/share/virtualenvs"

#__Steps are documented in the 'echo' commands below
clear
echo "__1__LIST env folders with a number, user enters env number"
ls $my_env_folder | cat -n
read a

clear
echo "\n__2__SET the selected FOLDER name"
my_env_selected=`ls $my_env_folder | head -n $a | tail -n 1`
my_env_selected=$my_env_folder/$my_env_selected
echo "     $my_env_selected\n"

echo "__3__CHANGE to the ACTUAL env folder name found in the envs '.project' file\n"
real_folder=`cat $my_env_selected/.project`
DIR=$( cd "${real_folder}" && pwd )
cd "${DIR}"
echo "__4__DONE!\n     CURRENT FOLDER: $PWD\n     NEXT: activate your virtual environment\n\n"
# IF all fails, the following line copies "cd folder_name" 
# Next, press CMD-V and then ENTER.
echo "cd $real_folder" | pbcopy

When you run the script it will list your virtual environments and ask you to enter the number of the virtual environment you want to use. Then it will read the actual folder name of that environment in the .storage file and change to that folder.


Enjoy!

More Webex Development related resources? https://cs.co/webexdevinfo 

Does the code not change to the right folder?

You could add the following lines to copy the destination foldername. So you only have to paste + press enter.

# IF all fails, the following line copies "cd folder_name" 
# Next, press CMD-V and then ENTER. 
echo "cd $real_folder" | pbcopy
Comments
davidn#
Cisco Employee
Cisco Employee

Dirk,

 

I tried it out but as soon as the script exits, I am still in the same directory where I executes the script.

__2__SET the selected FOLDER name
/Users/davidn/.local/share/virtualenvs/backend-mIHxTv_C

__3__CHANGE to the ACTUAL env folder name found in the envs '.project' file

__4__DONE!
CURRENT FOLDER: /Users/davidn/Desktop/workspace/CLUS_Demo/backend
NEXT: activate your virtual environment


davidn@DAVIDN-M-C7AA clus22 % pwd
/Users/davidn/Desktop/workspace/clus22
davidn@DAVIDN-M-C7AA clus22 %

Dirk-Jan Uittenbogaard
Cisco Employee
Cisco Employee

Hello @davidn# ,


Ah, that was one of my biggest challenges: change to a folder from a script. A script executes in a new shell and when it finished, you exit that shell and therefore go back to where you were before  

The method that I used works perfeclty on my Mac (MacOS 12.3.1) with my settings. 

 

Alternatively, at the end of the script your could add the following lines: 

# IF all fails, the following line copies "cd folder_name" 
# Next, press CMD-V and then ENTER. 
echo "cd $real_folder" | pbcopy

 

This copies the full path to your copy/paste buffer. As a result the only thing you have to do after running the script is paste + enter.

I will continue to look for alternative ways.

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Quick Links