sh 从当前目录向上搜索python virtualenv直到主目录,如果找到则将其获取。假设virtualenv名称为b

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 从当前目录向上搜索python virtualenv直到主目录,如果找到则将其获取。假设virtualenv名称为b相关的知识,希望对你有一定的参考价值。

#!/bin/bash
if [ "${BASH_SOURCE[0]}" = "\${0}" ]; then
	echo This script intends to be sourced rather than be called >> /dev/stderr
	exit 1
fi

__findrt_upward() {
	local acc=""
	local targetrt=""
	local venv="rt"  # virtualenv name to search for
	if [ -n "$1" ]; then expected_suffix="$1"; fi
	local targetrt=""

	# line 22-25 reverse the order of lines of stdin
	pwd \
	| tr '/' '\n' \
	| sed '/^ *$/d' \
	| while read pt; do
		acc="$acc/$pt"
		echo "$acc"
	done \
	| grep -n "" \
	| sed 's/^\([0-9]\+\):/\1 /' \
	| sort -k1 -nr \
	| sed 's/^\([0-9]\+\) //' \
	| while read pfx; do
		if [ "$pfx" = "$HOME" ]; then
			break
		fi
		if [ -f "$pfx/$venv/bin/activate" ]; then
			echo "$pfx/$venv/bin/activate"
			break
		fi
	done 
}

# don't do anything unless not in virtualenv
if [ -z "$VIRTUAL_ENV" ]; then
	__targetrt=$(__findrt_upward | head -1)
	if [ -f "$__targetrt" ]; then
		. "$__targetrt"
	else
		echo "virtualenv not found" >> /dev/stderr
	fi
	unset __targetrt
fi

unset __findrt_upward

以上是关于sh 从当前目录向上搜索python virtualenv直到主目录,如果找到则将其获取。假设virtualenv名称为b的主要内容,如果未能解决你的问题,请参考以下文章

VIM文本编辑器常用命令

Python从入门到放弃_核心模块方法

解决安装虚拟环境出现的问题(OSError: Command /home/python/.virtua...ngo3_web/bin/python3 - setuptools pkg_resource

确定当前Python环境中的site-packages目录位置

tsconfig.json

如何hook某一个shell命令