Python虚拟环境用于创建隔离的python环境,避免依赖和版本冲突,也间接处理权限问题。但是在 Ubuntu 中设置和使用它的最简单方法是什么?
pip install scrapy
Downloading/unpacking scrapy
Downloading Scrapy-0.24.2-py2-none-any.whl (502kB): 502kB downloaded
Downloading/unpacking pyOpenSSL (from scrapy)
Downloading pyOpenSSL-0.14.tar.gz (128kB): 128kB downloaded
Running setup.py (path:/home/elie/.virtualenvs/stat/build/pyOpenSSL/setup.py) egg_info for package pyOpenSSL
warning: no previously-included files matching '*.pyc' found anywhere in distribution
no previously-included directories found matching 'doc/_build'
Requirement already satisfied (use --upgrade to upgrade): six>=1.5.2 in /home/elie/.virtualenvs/stat/lib/python2.7/site-packages (from scrapy)
Downloading/unpacking queuelib (from scrapy)
Downloading queuelib-1.1.1.tar.gz
Running setup.py (path:/home/elie/.virtualenvs/stat/build/queuelib/setup.py) egg_info for package queuelib
Requirement already satisfied (use --upgrade to upgrade): lxml in /home/elie/.virtualenvs/stat/lib/python2.7/site-packages (from scrapy)
Downloading/unpacking …Run Code Online (Sandbox Code Playgroud) 在 16.04 的全新安装中,我尝试按照Gerhard Burger 的这个很棒的答案来安装 virtualenvwrapper 。
配置好后.bashrc,每次打开终端都会显示
bash: /usr/local/bin/python2.7: No such file or directory
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7 and that PATH is
set properly.
Run Code Online (Sandbox Code Playgroud)
脚本位置是,
$ sudo find / -name virtualenvwrapper.sh
[sudo] password for john:
/usr/local/bin/virtualenvwrapper.sh
Run Code Online (Sandbox Code Playgroud)
我的内容.bashrc是,
export WORKON_HOME=/home/john/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
export PIP_VIRTUALENV_BASE=/home/john/.virtualenvs
Run Code Online (Sandbox Code Playgroud)
这是我的 pip freeze
cffi==1.5.2
greenlet==0.4.9
pbr==1.10.0
readline==6.2.4.1
six==1.10.0
stevedore==1.15.0 …Run Code Online (Sandbox Code Playgroud) 有时需要创建一个退出virtualenv的副本来测试我们项目的额外功能。
在这种情况下,我们需要创建现有环境的副本并添加更多要求。
下面提到了创建现有 virtualenv 副本的一种方法:
# while using existing virtualenv
(oldenv): pip freeze > requirements.txt
# after creating and logged-in to new virtualenv
(newenv): pip install -r requirements.txt
# where requirements.txt is the same file created using (oldenv)
Run Code Online (Sandbox Code Playgroud)
但我个人并不喜欢这种方式。为什么??因为安装了我们可以重用的相同软件包。重复使用或说复制不仅会减少额外的时间和精力,还会将互联网带宽的使用减少到零,特别是对于有限和缓慢的互联网用户。
凉爽的!那么在本地创建现有virtualenv副本的其他方法是什么?
我知道我在下面提到的另外两种方式。希望这些会有所帮助。
我刚刚通过 deadsnakes ppa 在我的 18.04LTS 上安装了 python3.7:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.7 -y
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个虚拟环境,python3.7 -m venv env但我得到
Error: Command '['/path/to/desired/env/bin/python3.7', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
Run Code Online (Sandbox Code Playgroud)
python3 -m venv env0 以前工作得很好。
为什么会发生这种情况?
如何在 Ubuntu 18.04LTS 上为 python3.7 创建虚拟环境?
malikarumi@Tetouan2:~$ pip install virtualenv
Collecting virtualenv
Downloading virtualenv-12.0.7-py2.py3-none-any.whl (1.8MB)
100% |################################| 1.8MB 330kB/s
malikarumi@Tetouan2:~$ pip freeze
(a lot of stuff, but not virtualenv)
malikarumi@Tetouan2:~$ virtualenv testvenv1
The program 'virtualenv' is currently not installed. You can install it by typing:
sudo apt-get install python-virtualenv
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?是 python-virtualenv == pyvenv 吗?那不是还坏了吗?原来的 virtualenv 仍然适用于 python 吗?如果 venv(名称变体太多!!!)是标准库https://docs.python.org/3/library/venv.html 的一部分,为什么我被告知要安装它?
当我尝试安装它时,我得到了:
malikarumi@Tetouan2:~$ sudo apt-get install python-virtualenv
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed: …Run Code Online (Sandbox Code Playgroud) 在 installed 中python-virtualenv,因为这个问题说我应该使用 virtualenv 来安装 pygame。但是,我不确定这是如何完成的。
我做了什么(按照这些说明):
virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3 --no-pip
Run Code Online (Sandbox Code Playgroud)
然后我不知道去哪里。
如何安装要在 virtualenv 中使用的 pygame?
编辑:我按照 GuySoft 的说明进行操作,一切都安装得很好。但是,当我import pygame在python3中尝试时,出现以下错误:
>>> import pygame
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/alden/.virtualenvs/pywork3/lib/python3.3/site-packages/pygame/__init__.py", line 95, in <module>
from pygame.base import *
ImportError: /home/alden/.virtualenvs/pywork3/lib/python3.3/site-packages/pygame/base.cpython-33m.so: undefined symbol: PyCObject_Check
Run Code Online (Sandbox Code Playgroud) 我在 Ubuntu 17.04 上安装了 python 3.6。现在,我正在尝试使用命令创建虚拟环境:
python3.6 -m venv env
Run Code Online (Sandbox Code Playgroud)
但我收到以下消息:
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/makeev/test2/l/bin/python3.6', '-Im', 'ensurepip', '--upgrade', '--default-pip']
Run Code Online (Sandbox Code Playgroud)
sudo apt install python3-venv 没有帮助,我安装了这个包。
我阅读了类似的问题并尝试了他们的建议,但我仍然无法激活我的虚拟环境。目录的层次结构是:
myproject
-- virtualenv
-- startvenv.sh
Run Code Online (Sandbox Code Playgroud)
startvenv.sh 是:
#!/bin/bash
source virtualenv/bin/activate
Run Code Online (Sandbox Code Playgroud)
而且,我通过以下方式运行 startvenv.sh:
./startvenv.sh
Run Code Online (Sandbox Code Playgroud)
没有错误,但没有任何反应。为什么?理想情况下,我想打开一个新终端并在那里激活我的虚拟环境。
问题:
bin我用virtualenvwrapper(virtualenv在后台使用)生成的虚拟环境的目录位于子文件夹中,local而不是导致全面损坏(主要virtualenvwrapper是vscode目前)。基本上,这些工具期望解释器位于<envname>/bin而不是位于<envname>/local/bin.
信息:
我已经做了一些研究并发现了以下内容:
posix_local在模块中使用sysconfig而不是作为posix_prefix默认方案,这在此处进行了解释bin为local/bin虚拟环境内部以响应前缀,这是在这个问题posix_local中提出的,并产生了这个补丁,该补丁应该修复这个错误系统:
问题:
virtualenv再次安装我的环境<envname>/bin,忽略posix_local选项?virtualenv ×10
python ×8
pip ×3
bash ×2
python3 ×2
14.04 ×1
16.04 ×1
22.04 ×1
apt ×1
command-line ×1
copy ×1
python-2.7 ×1
scripts ×1