ubuntu创建虚拟环境步骤:
(1) 首先创建一个目录,即你想把虚拟环境放在哪里 (用已经存在的目录也可)
mkdir env/
(2) 利用命令行创建虚拟环境
virtualenv -p python3 env
虚拟环境创建成功的截图:
(3) 进入你所创建的虚拟环境
source env/bin/activate
(4) 关闭虚拟环境
deactivate
常见问题
如果你创建环境失败了,并显示下边的报错代码:
xuan:/home/ubuntu/studentAssign/software$ virtualenv -p python3 env
Running virtualenv with interpreter /home/ubuntu/studentAssign//.pyenv/shims/python3
Using base prefix '/home/ubuntu/studentAssign//.pyenv/versions/3.8.1'
/usr/lib/python3/dist-packages/virtualenv.py:1085: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
New python executable in /home/ubuntu/studentAssign/software/env/bin/python3
Also creating executable in /home/ubuntu/studentAssign/software/env/bin/python
Installing setuptools < 51.3, pkg_resources, pip < 21, wheel...
Complete output from command /home/ubuntu/student...ware/env/bin/python3 - "setuptools < 51.3" pkg_resources "pip < 21" wheel:
Exception:
Traceback (most recent call last):
File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/basecommand.py", line 209, in main
status = self.run(options, args)
File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/commands/install.py", line 267, in run
with self._build_session(options) as session:
File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/basecommand.py", line 66, in _build_session
session = PipSession(
File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/download.py", line 321, in __init__
self.headers["User-Agent"] = user_agent()
File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/download.py", line 93, in user_agent
zip(["name", "version", "id"], platform.linux_distribution()),
AttributeError: module 'platform' has no attribute 'linux_distribution'
Traceback (most recent call last):
File "<stdin>", line 24, in <module>
File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/__init__.py", line 217, in main
File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/basecommand.py", line 242, in main
File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/basecommand.py", line 66, in _build_session
File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/download.py", line 321, in __init__
File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/download.py", line 93, in user_agent
AttributeError: module 'platform' has no attribute 'linux_distribution'
----------------------------------------
...Installing setuptools < 51.3, pkg_resources, pip < 21, wheel...done.
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/virtualenv.py", line 2366, in <module>
main()
File "/usr/lib/python3/dist-packages/virtualenv.py", line 709, in main
create_environment(home_dir,
File "/usr/lib/python3/dist-packages/virtualenv.py", line 987, in create_environment
install_wheel(
File "/usr/lib/python3/dist-packages/virtualenv.py", line 918, in install_wheel
call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
File "/usr/lib/python3/dist-packages/virtualenv.py", line 810, in call_subprocess
raise OSError(
OSError: Command /home/ubuntu/student...ware/env/bin/python3 - "setuptools < 51.3" pkg_resources "pip < 21" wheel failed with error code 1
面对上述的问题,你可以更新一下pip版本,具体命令如下:
$ sudo pip3 install --upgrade pip
更新完之后,再重新创建虚拟环境就可以了
$ virtualenv -p python3 xiaenv
以上是关于虚拟环境比较基础的操作,希望可以帮到你~