Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I activate virtual environment in python shell

I have created a virtual environment (directory is env) and have installed some third party packages. I work with Mac OSX. How can I activate my venv in the python interactive shell? I tried

source env/bin/activate

and get the error message: SyntaxError: invalid syntax

I have seen some had posted this question 8 years ago but I haven't found what I was looking for...I have also checked the python documentation but it is not clear for me how to activate it

https://docs.python.org/3/tutorial/venv.html?highlight=virtual

Can anybody please help?

like image 318
aurumpurum Avatar asked Nov 05 '25 22:11

aurumpurum


1 Answers

In your shell, normally python will point you to a default instance of python:

python

Python 2.7.14 (default, Sep 25 2017, 09:53:22)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

This is because python is located in /usr/local/bin which is part of your default PATH. Now, when you source activate <some_env>, you are modifying PATH, which you can actually see in the /path/to/venv/some_env/bin/activate file:

#!/bin/bash

...

    if [[ $PATH == *"CONDA_PATH_PLACEHOLDER"* ]]; then
        # If it did, replace it with our _NEW_PART
        export PATH="$($_CONDA_PYTHON -c "import re; print(re.sub(r'CONDA_PATH_PLACEHOLDER', r'$_NEW_PART', '$PATH', 1))")"
    else

        #### HERE IS WHERE THAT PATH IS OVERWRITTEN

        export PATH="$_NEW_PART:$PATH"
    fi

    # CONDA_DEFAULT_ENV is the shortest representation of how conda recognizes your env.
    #    It can be an env name, or a full path.
    #    Last date of change: 2016-06-21
    # If the string contains / it's a path
    if [[ "$@" == */* ]]; then
        export CONDA_DEFAULT_ENV=$(get_abs_filename "$args")
    else
        export CONDA_DEFAULT_ENV="$args"
    fi
...
esac

I'm using anaconda but the concept is the same, and ... indicates where I've snipped out parts of the script. It is effectively exporting the venv python executable path into PATH. So you won't be pointing to your venv python unless one of two conditions arises: a) you explicitly call that python executable or b) you source activate myenv and then call python.

So the correct workflow is:

source /path/to/myenv/bin/activate myenv

Then call python

As an example, the first call to $PATH is before activating my conda env, the second is after:

➜  ~ echo $PATH
/Users/mm92400/bin:/usr/local/bin:/Users/mm92400/anaconda3/bin:/Users/mm92400/.cargo/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands
➜  ~ source activate py36
(py36) ➜  ~ echo $PATH
/Users/mm92400/anaconda3/envs/py36/bin:/Users/mm92400/bin:/usr/local/bin:/Users/mm92400/anaconda3/bin:/Users/mm92400/.cargo/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands

note how $PATH is different

like image 165
C.Nivs Avatar answered Nov 08 '25 13:11

C.Nivs