Using Python Virtual Environment in Git Bash

I recently installed Python on a Windows 10 system, and then realized that I also installed Git Bash a while ago. Git Bash's Linux commands are very useful, so I wanted to try using a Python virtual environment on Git Bash.

Using Python in Git Bash

If you only want to use Python and do not need a virtual environment, simply type winpty python, and you don't need to read the rest of the content The reason for adding winpty here is that it can support interactive operations. Please refer to this article for more information.

Using Python Virtual Environment in Git Bash

Exploration has found that a launcher suitable for Bash has been automatically generated when installing the virtual environment, so the following methods can be used to start the virtual environment in Git Bash:

Open Git Bash and run the launcher (replace it with your own path here):
source /d/myenvs/test/Scripts/actitvate.

The appearance of (virtual environment name) indicates entering the virtual environment, where (test) indicates that the virtual environment name is test:

For ease of use, set an alias for the above command by adding the following statement
alias env_test='source /d/myenvs/test/Scripts/actitvate'
to~/. bashrc. Now, simply type env_test in Git Bash to open the test virtual environment.

To exit the virtual environment, type deactivate.

If multiple virtual environments are created, in order to facilitate switching between different virtual environments, you can create a new shell script, such as myenv.sh (the author's path is/d/myenvs/myenv.sh, you can choose the path yourself), and type the following content in it.

#!/bin/bash
cd work path 
ENV_NAME=$1 #virtual environment name 
echo "You are in the virtualvenv '$ENV_NAME'"
source /d/myenvs/$ENV_NAME/Scripts/activate

Set another alias for the process of calling this script.

alias myenv='source /d/myenvs/myenv.sh'

To use a virtual environment in this way, simply type in Git Bash.

myenv environment name 

Using iPhone in Git Bash

To use iPhone, simply enter winpty ipython directly:

PS: If winpty is omitted here and only ipython is typed, iPhone can still be used. However, the shortcut key Ctrl+d for exiting iPhone is no longer valid, so adding winpty is more convenient.

Tips

  1. When using Chrome browser to switch from Notebook to a Console page, it often lags, but using Edge does not have this problem. I don't know why