修复 Ubuntu 上找不到 mkvirtualenv 命令 |修复 Ubuntu 上找不到 mkvirtualenv 命令 |修复 Ubuntu 上找不到 mkvirtualenv 命令 |修复 Ubuntu 上找不到 mkvirtualenv 命令 |
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

修复 Ubuntu 上找不到 mkvirtualenv 命令 |

发表 admin at 2025年2月28日
类别
  • 未分类
标签

在这个简短的教程中,我们将向您展示如何在 Ubuntu Linux 计算机上安装 mkvirtualenv(virtualenvwrapper 和 virtualenv)。 virtualenvwrapper 是一组以 Bourne shell 兼容语法定义的 shell函数。 virtualenv 使您能够在 Linux/macOS 系统中创建许多不同的 Python 环境。使用 virtualenvwrapper,您可以在 Linux 或 OS X 计算机上的以下 shell 下自动运行测试。

    bash
    ksh
    zsh

这是当我尝试在终端中输入 mkvirtualenv 时遇到的错误。

$ mkvirtualenv
mkvirtualenv: command not found

您需要将 virtualenvwrapper 包安装到安装 virtualenv 的同一全局站点包区域中。此安装需要管理权限。确保您以 root 用户或具有 sudo 权限的标准用户身份登录。

更新系统包列表。

sudo apt update

然后安装Python构建工具。

# Python 2
sudo apt-get install python-pip python-dev build-essential

# Python 3
sudo apt-get install python3-pip python3-dev build-essential

然后安装virtualenv和virtualenvwrapper软件包。

使用 APT 包管理器:

sudo apt install virtualenv virtualenvwrapper

与点:

# Python 2
sudo pip install virtualenv virtualenvwrapper

# Python 3
sudo pip3 install virtualenv virtualenvwrapper

将其安装到全局站点包中的另一种方法是将其添加到您的用户本地目录(通常是 ~/.local)。请参见下面的示例:

$ pip install --user virtualenv virtualenvwrapper

配置你的外壳

根据您的安装方法,您可能需要将 virtualenvwrapper 脚本路径添加到启动文件(.bashrc、.profile 等)。

但首先要确认virtualenvwrapper脚本的位置。

$ sudo find / -name virtualenvwrapper.sh
/usr/share/virtualenvwrapper/virtualenvwrapper.sh

然后编辑 ~/.bashrc 并添加一行到 source /usr/share/virtualenvwrapper/virtualenvwrapper.sh

$ vim ~/.bashrc
source '/usr/share/virtualenvwrapper/virtualenvwrapper.sh'

现在获取您的 bashrc 文件。

$ source ~/.bashrc
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postactivate
virtualenvwrapper.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_venv
created 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,XonshActivator
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/preactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/postactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/get_env_details

要随时运行项目虚拟环境:

$ workon test_venv

退出运行:

$ deactivate

您现在应该能够使用 virtualenv 和 virtualenvwrapper 脚本来处理您的 Python 项目。

其他Python相关文章:

  • 如何在 Ubuntu 上使用 Virtualenv 安装 Python 2
  • 使用 Apache 和 mod_wsgi 在 CentOS 7 上部署 Python 3 Django 应用程序
  • 学习 Python 编程的最佳书籍
©2015-2025 Norria support@norria.com