为什么 VS Code 的集成终端会忽略 conda activate,但会尊重 .bashrc 中 Poetry 路径的更新?

问题描述 投票:0回答:1

我在 Linux 系统上使用 Visual Studio Code 的集成终端时遇到问题。尽管终端显然采购了我的

.bashrc
文件(通过
echo
语句确认),但它似乎有选择地应用配置。具体来说,它尊重对 Poetry 所做的
PATH
修改,但忽略
conda activate
命令。

我希望我的集成终端在使用 VS Code ssh-remote 时与我的普通终端尽可能相似。我不明白为什么 VS Code 集成终端似乎在挑选哪些东西放在我的 PATH 中以及哪些东西不放在我的 PATH 中。

.bashrc
的相关部分:

# Setting up Poetry
export VENV_PATH=$HOME/.virtualenvs/venv_for_poetry
export PATH="$VENV_PATH/bin:$PATH"

# Conda initialization
__conda_setup="$('$HOME/miniconda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "$HOME/miniconda/etc/profile.d/conda.sh" ]; then
        . "$HOME/miniconda/etc/profile.d/conda.sh"
    else
        export PATH="$HOME/miniconda/bin:$PATH"
    fi
fi
unset __conda_setup

# Activating conda environment
conda activate snap_cluster_setup_py311

症状:

  • 在 VS Code 中打开终端时,
    PATH
    已正确修改为包含 Poetry 环境。
  • 但是,Conda 环境
    snap_cluster_setup_py311
    未激活,即使
    echo
    语句确认行
    conda activate snap_cluster_setup_py311
    已执行。

我已经尝试过:

  1. 通过添加

    .bashrc
    命令确保
    echo
    的来源。

  2. 配置 VS Code 的

    settings.json
    以显式使用 bash 和源
    .bashrc
    :

    "terminal.integrated.profiles.linux": {
        "bash": {
            "path": "/bin/bash",
            "args": ["-i"]
        }
    },
    "terminal.integrated.defaultProfile.linux": "bash"
    
  3. 使用两种

    terminal.integrated.inheritEnv
    设置(true 和 false)进行测试。

  4. 在 Conda 和 Poetry 设置周围添加了额外的调试输出。

  5. 仅使用 Conda 和 Poetry 设置创建了一个最小的

    .bashrc
    ,观察到了相同的行为。

  6. 我也尝试过:

        "workbench.startupEditor": "none",
        "terminal.integrated.profiles.linux": {
            "bash": {
                "path": "/bin/bash",
                "args": ["-i", "-c", "exec bash --rcfile ~/.bashrc"]
            }
        },
        "terminal.integrated.defaultProfile.linux": "bash"
    

    这不起作用,但它确实回显了两次 NEW :

    NEW
    NEW
    (snap_cluster_setup_py311) brando9@ampere1~ $ echo $PATH | tr ':' '\n'
    /lfs/ampere1/0/brando9/.virtualenvs/venv_for_poetry/bin
    /lfs/ampere1/0/brando9/.elan/bin
    /afs/cs/software/bin
    /lfs/ampere1/0/brando9/.virtualenvs/venv_for_poetry/bin
    /lfs/ampere1/0/brando9/.elan/bin
    /afs/cs/software/bin
    /lfs/ampere1/0/brando9/.vscode-server-insiders/cli/servers/Insiders-5ae5a741bdb676abb667fd4896dc2a40e313db3a/server/bin/remote-cli
    /lfs/ampere1/0/brando9/miniconda/envs/snap_cluster_setup_py311/bin
    /lfs/ampere1/0/brando9/miniconda/condabin
    /lfs/ampere1/0/brando9/.virtualenvs/venv_for_poetry/bin
    /lfs/ampere1/0/brando9/.elan/bin
    /usr/kerberos/sbin
    /usr/kerberos/bin
    /afs/cs/software/sbin
    /afs/cs/software/bin
    /usr/local/sbin
    /usr/local/bin
    /usr/sbin
    /usr/bin
    /sbin
    /bin
    /usr/games
    /usr/local/games
    /snap/bin
    /opt/puppetlabs/bin
    

也是我的初始用户 VS Code

settings.json
:

{
    "workbench.colorTheme": "Visual Studio Dark",
    "editor.inlineSuggest.enabled": true,
    "workbench.colorCustomizations": {},
    "explorer.confirmDragAndDrop": false,
    "terminal.integrated.enableMultiLinePasteWarning": false,
    "[python]": {
        "diffEditor.ignoreTrimWhitespace": false,
        "editor.formatOnType": true,
        "editor.wordBasedSuggestions": "off"
    },
    "github.copilot.enable": {
        "*": false,
        "plaintext": false,
        "markdown": false,
        "scminput": false,
        "lean4": true,
        "python": true
    },
    "terminal.integrated.inheritEnv": false,
    "window.zoomLevel": -1,
    "python.analysis.fixAll": [
        
    ],
    "debug.focusEditorOnBreak": false,
    "accessibility.signals.lineHasInlineSuggestion": {
        "sound": "off"
    },
    "workbench.startupEditor": "none"
}

为什么 VSCode 的终端应用 Poetry 的 PATH 更改但不激活 Conda 环境?是什么导致了

.bashrc
设置的选择性应用,以及如何确保 Conda 环境正确激活?

还发布于Discord

visual-studio-code vscode-debugger vscode-remote
1个回答
-1
投票

黑客解决方案。如果子字符串路径已经存在,我告诉我的

.bashrc
不要编辑我的
PATH

...
# Check if the CS software directory is already in the PATH. If not, prepend it to access CS software tools first.
if [[ ":$PATH:" != *":/afs/cs/software/bin:"* ]]; then
    echo "Adding CS software tools to PATH"
    export PATH="/afs/cs/software/bin:$PATH"
fi
...
# Check if the Elan tools directory is already in the PATH. If not, prepend it to ensure Elan tools are prioritized.
if [[ ":$PATH:" != *":$HOME/.elan/bin:"* ]]; then
    echo "Adding Elan tools to PATH"
    export PATH="$HOME/.elan/bin:$PATH"
fi
...
# Check if the Poetry virtual environment path is already in the PATH. If not, prepend it to ensure the Poetry environment is prioritized.
if [[ ":$PATH:" != *":$VENV_PATH/bin:"* ]]; then
    echo "adding virtualenvs to path"
    export PATH="$VENV_PATH/bin:$PATH"
fi
...
conda activate snap_cluster_setup_py311

这似乎对我有用:

(snap_cluster_setup_py311) brando9@ampere1~ $ echo $PATH | tr ":" "\n"
/lfs/ampere1/0/brando9/.vscode-server-insiders/cli/servers/Insiders-5ae5a741bdb676abb667fd4896dc2a40e313db3a/server/bin/remote-cli
/lfs/ampere1/0/brando9/miniconda/envs/snap_cluster_setup_py311/bin
/lfs/ampere1/0/brando9/miniconda/condabin
/lfs/ampere1/0/brando9/.virtualenvs/venv_for_poetry/bin
/lfs/ampere1/0/brando9/.elan/bin
/usr/kerberos/sbin
/usr/kerberos/bin
/afs/cs/software/sbin
/afs/cs/software/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games
/snap/bin
/opt/puppetlabs/bin

虽然我真的不认为这是一个好的解决方案。 VScode 不应该挑选如何运行我的

.bashrc
。如果它再次运行,那么 所有内容都应该按正确的顺序添加,尤其是我的 conda env 。但我想它并没有做到这一点。否则调试器将无法工作等等。

当前

.bashrc

#!/bin/bash
# When you log in to snap it runs .bash_profile
# Since it doesn't let you edit that file to source YOUR real .bashrc configurations, you need to inspect it and observe it is sourcing ~/.bashrc
# Since it is sourcing `. ~/.bashrc  you need to create that file at ~ manually every time you want to use a new server
# (this is because the first time you use a server that file doesn't exist yet at ~ (i.e. $HOME), which points to the root of the lfs server your setting up)
# My trick is to create a single .bashrc at /afs/cs.stanford.edu/u/brand9/.bashrc
# Then manually create a soft link to point to the file every time I set up a new server e.g.,
# if the working version is on afs
# ln -s /afs/cs.stanford.edu/u/brando9/.bashrc $HOME/.bashrc
# or somehwere else:
# ln -s /lfs/mercury1/0/brando9/evals-for-autoformalization/.bashrc $HOME/.bashrc
# then do cd $HOME; ls -lah to confirm the soft link point to where you expect:
# e.g., .bashrc -> /afs/cs.stanford.edu/u/brando9/.bashrc
# confirm your at the server you expect
# hostname e.g., output mercury1.stanford.edu
# now you can put whatever you want in your .bashrc (located at afs and soft linked at ~ for this server)!
# e.g., you can activate the conda env and install the cuda versions you need want
# note: the cuda and conda versions you need might depend on the specific server since the specific cuda version might only work for the gpu that server has

# ---- Brando's scaffold .bashrc file ----

# - Approximately source the sys admin's .bashrc (located at /afs/cs/etc/skel/.bashrc)
# since snap is set up badly and it needs the reauth command to re-authenticate randomly somtimes, you need to make sure reauth cmd is available
# Check if the CS software directory is already in the PATH. If not, prepend it to access CS software tools first.
if [[ ":$PATH:" != *":/afs/cs/software/bin:"* ]]; then
    echo "Adding CS software tools to PATH"
    export PATH="/afs/cs/software/bin:$PATH"
fi
# 
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Loads all readable shell scripts from /afs/cs/etc/profile.d/ if the current shell is not a login shell.
#if [ "x$SHLVL" != "x1" ]; then # We're not a login shell
    for i in /afs/cs/etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            . $i
        fi
    done
#fi

# Sets the maximum number of open file descriptors for the current process to 120,000.
ulimit -n 120000
# commented out so sourcing .bashrc doesn't output things to screen.
#ulimit -Sn # to check it's 120000
#ulimit -Hn # to check it's 120000

# - The defaul $HOME is /afs/cs.stanford.edu/u/brando9 but since you want to work in a specific server due to otherwise conda being so hard you need to reset what home is, see lfs: https://ilwiki.stanford.edu/doku.php?id=hints:storefiles#lfs_local_server_storage  
export LOCAL_MACHINE_PWD=$(python3 -c "import socket;hostname=socket.gethostname().split('.')[0];print('/lfs/'+str(hostname)+'/0/brando9');")
mkdir -p $LOCAL_MACHINE_PWD
export WANDB_DIR=$LOCAL_MACHINE_PWD
export HOME=$LOCAL_MACHINE_PWD

# - set up afs short cuts
# since you are loged in to afs this moves you to your local computer
cd $HOME
export TEMP=$HOME
export AFS=/afs/cs.stanford.edu/u/brando9
export DFS=/dfs/scratch0/brando9/

# - prompt colours
BLACK='\e[0;30m'
RED='\e[0;31m'
GREEN='\e[0;32m'
BROWN='\e[0;33m'
BLUE='\e[0;34m'
PURPLE='\e[0;35m'
CYAN='\e[0;36m'
LIGHT_GREY='\e[0;37m'
DARK_GREY='\e[1;30m'
LIGHT_RED='\e[1;31m'
LIGHT_GREEN='\e[1;32m'
YELLOW='\e[1;33m'
LIGHT_BLUE='\e[1;34m'
LIGHT_PURPLE='\e[1;35m'
LIGHT_CYAN='\e[1;36m'
WHITE='\e[1;37m'

BACK_DEFAULT_COLOR='\e[m'

HOST_PART='$(hostname | cut -d. -f1)'
export PS1="\[$LIGHT_GREY\]\u@$HOST_PART\[$LIGHT_GREEN\]\w\[$LIGHT_GREY\] \$ \[$LIGHT_CYAN\]"

# -- If you use wandb you might need this:
export WANDB_API_KEY=TODO
export HF_TOKEN='your_token_here'
export OPENAI_KEY='your_openai_key_here'

# - Start this linux env with the gpu with most memory available
export CUDA_VISIBLE_DEVICES=5
export LEAST_GPU_ID=$(nvidia-smi --query-gpu=memory.used --format=csv,nounits,noheader | awk '{print NR-1 " " $1}' | sort -nk2 | head -n1 | cut -d' ' -f1)
export CUDA_VISIBLE_DEVICES=$LEAST_GPU_ID

# -- Lean, ref: https://github.com/brando90/snap-cluster-setup?tab=readme-ov-file#lean-in-snap
# Check if the Elan tools directory is already in the PATH. If not, prepend it to ensure Elan tools are prioritized.
if [[ ":$PATH:" != *":$HOME/.elan/bin:"* ]]; then
    echo "Adding Elan tools to PATH"
    export PATH="$HOME/.elan/bin:$PATH"
fi

# # not needed, if issues see: https://ilwiki.stanford.edu/doku.php?id=hints:gpu
# export PATH=/usr/local/cuda-11.7/bin:$PATH
# export LD_LIBRARY_PATH=/usr/local/cuda-11.7/lib64:$LD_LIBRARY_PATH

# -- poetry, we are setting up poetry before conda so that we don't accidentally use poetry's python version, see: https://github.com/brando90/snap-cluster-setup?tab=readme-ov-file#poetry
# assumes mkdir $HOME/.virtualenvs has been ran
export VENV_PATH=$HOME/.virtualenvs/venv_for_poetry
# assume poetry has been installed as explained here: https://github.com/brando90/snap-cluster-setup?tab=readme-ov-file#poetry
# Check if the Poetry virtual environment path is already in the PATH. If not, prepend it to ensure the Poetry environment is prioritizede. ref: https://chat.openai.com/c/500163c3-086a-426b-b5ca-ade6b4819c95
if [[ ":$PATH:" != *":$VENV_PATH/bin:"* ]]; then
    echo "adding virtualenvs to path"
    export PATH="$VENV_PATH/bin:$PATH"
fi

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('$HOME/miniconda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "$HOME/miniconda/etc/profile.d/conda.sh" ]; then
        . "$HOME/miniconda/etc/profile.d/conda.sh"
    else
        export PATH="$HOME/miniconda/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

# very similar to the conda init code above, will leave it out since snap seems to be working e.g., it can find conda command just fine with the above code, note above code **does** have to be at the end
# export PATH="$HOME/miniconda/bin:$PATH"

# activates the conda base env
source $HOME/miniconda/bin/activate

# activate snap_cluster_setup default conda env
conda activate snap_cluster_setup_py311
# echo $PATH | tr ':' '\n'

# nvhtop: https://github.com/peci1/nvidia-htop, RS: https://discord.com/channels/488822282664280084/489073165851688960/1230951660671602818
if ! command -v nvidia-htop.py &> /dev/null; then
    pip install nvidia-htop
fi
alias nvhtop='nvidia-htop.py --color -l 100'
© www.soinside.com 2019 - 2024. All rights reserved.