简体   繁体   English

Python虚拟环境未从bash脚本激活

[英]Python virtual environment not activating from bash script

Now before you mark this as a duplicate, I have tried the solution posted here and they aren't working for me. 现在,在您将其标记为重复项之前,我已经尝试过此处发布的解决方案,但它们对我不起作用。 I tried making an alias and I tried creating a function as so: 我尝试制作一个别名,然后尝试创建一个函数,如下所示:

activate () {
    echo Activating Virtual Environment...
    source alexa/bin/activate
}
activate

But my script just gets run through without a virtual environment getting activated. 但是我的脚本只是在没有激活虚拟环境的情况下运行。 The script is being run from the same directory as my virtual environment directory, alexa . 该脚本是从与我的虚拟环境目录alexa相同的目录中运行的。

For clarity, the other solution I tried was to make an alias: 为了清楚起见,我尝试的另一个解决方案是使用别名:

alias activate="source alexa/bin/activate"
activate

That didn't work and gave me an error that ./alexaEnvSetup.sh: line 43: activate: command not found . 那没有用,给了我一个错误./alexaEnvSetup.sh: line 43: activate: command not found

Any thoughts or ideas? 有什么想法或想法吗?

EDIT : I think it is worth mentioning that the echo command does print out when I do this. 编辑 :我认为值得一提的是,执行此操作时echo命令确实会打印出来。 So the function is getting entered. 因此正在输入该功能。 The virtual environment is just not getting activated. 虚拟环境只是没有被激活。

EDIT : Adding full code: 编辑 :添加完整的代码:

#!/bin/bash

if [[ "$OSTYPE" == "linux-gnu" ]]; then
    echo Operating system: Linux
elif [[ "$OSTYPE" == "darwin"* ]]; then
    echo Operating system: Mac OSX
    echo

    # Install Python 3.6.5 using `curl`
    curl -O https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
    tar xf Python-3.6.5.tgz
    cd Python-3.6.5
    ./configure
    make
    make install

    echo
    echo Python Version 3.6.5 Installed
    echo

    # Install Pip
    curl -O http://bootstrap.pypa.io/get-pip.py
    /usr/local/bin/python3.6 get-pip.py

    echo
    echo Pip Installed
    echo

    # Install virtualenv
    pip install virtualenv

    echo
    echo Virtual Environment Installed

    virtualenv -p python3 alexa
    echo Created Virtual Environment, \"alexa\"

    activate () {
        echo Activating Virtual Environment...
        source /Users/XXXX/Auto-Lab/Commerce/alexa/bin/activate
    }
    export -f activate
    activate

    echo Virtual Environment, \"alexa\", Created and Activated
    echo

    # All packages (time, urllib, and json) should come default with Python3

elif [[ "$OSTYPE" == "cygwin" ]]; then
    # POSIX compatibility layer and Linux environment emulation for Windows
    echo Operating system: Cygwin
elif [[ "$OSTYPE" == "msys" ]]; then
    # Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
    echo Operating system: Msys
elif [[ "$OSTYPE" == "win32" ]]; then
    echo Operating system: Windows32
elif [[ "$OSTYPE" == "freebsd"* ]]; then
    echo Operating system: FreeBSD
else
    echo Operating system unknown.
fi
  1. use the full path to the activate script in the function: source /path/to/activate 在函数中使用激活脚本的完整路径source /path/to/activate
  2. export the function: export -f activate 导出功能: export -f activate
  3. ensure the script is a bash script: #!/bin/bash 确保脚本是bash脚本: #!/bin/bash

The following works for me using Anaconda virtual env, perhaps it will work with yours also? 以下使用Anaconda虚拟环境对我有用,也许它也可以与您一起使用?

#!/usr/bin/env bash

# do bash stuff

# Python env
PATH=/home/username/path/to/activate/bin
python -u /script/to/run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM