本文将简单介绍如何 在Ubuntu Linux机器上 安装 mkvirtualenv (virtualenvwrapper和virtualenv)。 virtualenvwrapper 是使用 Bourne Shell兼容语法定义的 一组Shell 函数 。 virtualenv使您可以在Linux / macOS系统中创建许多不同的Python环境。 使用virtualenvwrapper,可以自动在Linux或OS X计算机上的以下Shell下运行测试。
如果尝试 在终端中 键入 mkvirtualenv , 这是我得到的“command not found”错误,即找不到命令:
$ mkvirtualenvmkvirtualenv: command not found
这时需要将virtualenvwrapper软件包安装到与安装virtualenv相同的全局站点软件包区域中。此安装需要管理特权。确保您以root用户或具有sudo特权的标准用户身份登录。
更新系统软件包列表。
sudo apt update
然后安装Python构建工具。
# Python 2sudo apt-get install python-pip python-dev build-essential# Python 3sudo apt-get install python3-pip python3-dev build-essential
然后安装 virtualenv 和 virtualenvwrapper 软件包。
使用APT软件包管理器:
sudo apt install virtualenv virtualenvwrapper
使用Pip:
# Python 2 $ sudo pip install virtualenv virtualenvwrapper # Python 3 $ sudo pip3 install virtualenv virtualenvwrapper
将其安装到全局站点程序包中,另一种方法是将其添加到 用户本地目录 (通常为〜/ .local)中。 请参见以下示例:
$ pip install --user virtualenv virtualenvwrapper
配置您的命令行管理程序
根据安装方法,可能需要 在启动文件( .bashrc , .profile 等)中 添加 virtualenvwrapper 脚本路径 。但是首先要确认virtualenvwrapper脚本的位置。
$ sudo find / -name virtualenvwrapper.sh/usr/share/virtualenvwrapper/virtualenvwrapper.sh
然后编辑 〜/ .bashrc 并在源代码 /usr/share/virtualenvwrapper/virtualenvwrapper.sh中 添加一行
$ vim ~/.bashrcsource '/usr/share/virtualenvwrapper/virtualenvwrapper.sh'
现在获取您的 bashrc 文件。
$ source ~/.bashrcvirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/premkprojectvirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postmkprojectvirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/initializevirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/premkvirtualenvvirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postmkvirtualenvvirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/prermvirtualenvvirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postrmvirtualenvvirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/predeactivatevirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postdeactivatevirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/preactivatevirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postactivatevirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/get_env_details
你也可以将
WORKON_HOME
变量
设置
为默认路径为的自定义路径
$HOME/.virtualenvs
。
export WORKON_HOME=/my/other/path
如果再次加载virtualenvwrapper时该目录不存在,则会自动创建该目录。
尝试运行 mkvirtualenv 命令:
$ mkvirtualenv -p /usr/bin/python3 test_venvcreated virtual environment CPython3.8.2.final.0-64 in 448ms creator CPython3Posix(dest=/home/computingforgeeks/.virtualenvs/test_venv, clear=False, global=False) seeder FromAppData(download=False, pep517=latest, requests=latest, appdirs=latest, progress=latest, lockfile=latest, certifi=latest, urllib3=latest, packaging=latest, pyparsing=latest, chardet=latest, six=latest, pip=latest, colorama=latest, CacheControl=latest, pkg_resources=latest, webencodings=latest, ipaddr=latest, distlib=latest, contextlib2=latest, html5lib=latest, pytoml=latest, setuptools=latest, msgpack=latest, wheel=latest, idna=latest, distro=latest, retrying=latest, via=copy, app_data_dir=/home/computingforgeeks/.local/share/virtualenv/seed-app-data/v1.0.1.debian) activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivatorvirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/predeactivatevirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/postdeactivatevirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/preactivatevirtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/postactivate
随时开启虚拟运行环境,命令如下:
$ workon test_venv
退出运行:
$ deactivate
现在应该可以使用 virtualenv 和 virtualenvwrapper 脚本 在Python项目上工作,也不会出现 command not found(找不到命令)错误。
备注:以上文章来源惠主机,设置过程仅供参考,如有任何疑问或问题,欢迎交流讨论!
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69924872/viewspace-2731814/ ,如需转载,请注明出处,否则将追究法律责任。