使用 python3 创建 virtualenv

Creating virtualenv with python3

我一般用python2.7做项目。

对于一个项目,我需要使用python 3.5+

我在 Mac 上安装了 python3。

还使用 pip3 安装了 virtualenv

现在当我运行命令

virtualenv -p python3 test

我收到以下错误:

Running virtualenv with interpreter /usr/bin/python3
Already using interpreter /Library/Developer/CommandLineTools/usr/bin/python3
Using base prefix '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7'
New python executable in /Users/sourabh/virtualenvs/test/bin/python3
Also creating executable in /Users/sourabh/virtualenvs/test/bin/python
Traceback (most recent call last):
  File "/Library/Python/3.7/site-packages/virtualenv.py", line 2632, in <module>
    main()
  File "/Library/Python/3.7/site-packages/virtualenv.py", line 870, in main
    symlink=options.symlink,
  File "/Library/Python/3.7/site-packages/virtualenv.py", line 1156, in create_environment
    install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages=site_packages, clear=clear, symlink=symlink)
  File "/Library/Python/3.7/site-packages/virtualenv.py", line 1621, in install_python
    shutil.copy(original_python, py_executable)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/shutil.py", line 245, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/shutil.py", line 103, in copyfile
    if _samefile(src, dst):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/shutil.py", line 88, in _samefile
    return os.path.samefile(src, dst)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/genericpath.py", line 96, in samefile
    s1 = os.stat(f1)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

用 Python 3

制作虚拟环境

用它来制作 env

python3 -m venv env

激活env

source env/bin/activate

改进 @ParthS007 的回答(想对现有答案添加评论但由于信誉不足而无法):

  1. 将虚拟环境名称更正为常用名称venv,以便像我这样的初学者更容易use/understand结合其他教程。
  2. 添加了如何停用和删除虚拟环境。

For making a virtual environment with Python 3

Use this for making env

python3 -m venv venv

To activate env

source env/bin/activate

停用环境

deactivate

删除环境

rm -rf venv