Sharing Python virtualenv environments

前端 未结 2 1431
忘了有多久
忘了有多久 2021-02-13 11:31

I have a Python virtualenv (created with virtualenvwerapper) in one user account. I would like to use it from another user account on the same host.

How can I do this?

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-13 12:14

    I had to do this for workmates. The @Flavius answer worked great once I added a few commands to handle virtualenvwrapper. You need to put your venvs and your WORKON projects folder some place you and your boss/friend can find and use.

    sudo mkdir -p /usr/local/share
    sudo mv ~/.virtualenvs /usr/local/share
    sudo mkdir -p /usr/src/venv/
    

    Assuming you want everyone on the machine to be able to both mkproject and workon:

    chmod a+rwx /usr/local/share/.virtualenvs
    chmod a+rwx /usr/src/venv
    

    Otherwise chown and chmod to match your security requirements.

    If you have any hooks or scripts that expect ~/.virtualenvs to be in the normal place, you better symlink it (on both your user account and your friend's)

    ln -s /usr/local/share/.virtualenvs ~/.virtualenvs
    

    Then modify your (and your friend's) .bashrc file to let virtualenvwrapper know where you moved things. Your bashrc should have something like this:

    export PROJECT_HOME="/usr/src/venv/"
    export WORKON_HOME="/usr/local/share/.virtualenvs"
    
    export USR_BIN=$(dirname $(which virtualenv))
    if [ -f $USR_BIN/virtualenvwrapper.sh ]; then
        source $USR_BIN/virtualenvwrapper.sh
    else
        if [ -f /usr/bin/virtualenvwrapper.sh ]; then
            source /usr/bin/local/virtualenvwrapper.sh
        else
            echo "Can't find a virtualenv wrapper installation"
        fi  
    fi
    

    Once you log out and back in (or just source ~/.bashrc you should be good to go with commands like mkproject awesome_new_python_project and workon awesome_new_python_project.

    As a bonus, add hooks to load the project folder in sublime every time your workon.

提交回复
热议问题