简体   繁体   English

如何通过virtualenv创建python2.7环境?

[英]How to create python2.7 environment by virtualenv?

I am using python on MacOS and I have a problem to setup a python2.7 environment. 我在MacOS上使用python,我在设置python2.7环境时遇到问题。 I have installed both python2.7 and python3.6 on the system. 我在系统上安装了python2.7和python3.6。 And run below code to setup python2.7 env. 并运行下面的代码来设置python2.7 env。 I am not sure why I get this error. 我不知道为什么会出现这个错误。 From the output I see it is using /anaconda/lib/python3.6 directory. 从输出我看到它是使用/anaconda/lib/python3.6目录。 How can I make it to use python2.7 environment? 如何才能使用python2.7环境?

$ virtualenv --python=/usr/bin/python2.7 venv/
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /Users/joey/dev/jump/jump-api/venv/bin/python2.7
Traceback (most recent call last):
  File "/anaconda/lib/python3.6/site-packages/virtualenv.py", line 2869, in <module>
    main()
  File "/anaconda/lib/python3.6/site-packages/virtualenv.py", line 713, in main
    symlink=options.symlink)
  File "/anaconda/lib/python3.6/site-packages/virtualenv.py", line 925, in create_environment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "/anaconda/lib/python3.6/site-packages/virtualenv.py", line 1231, in install_python
    copy_exe_shared_libs_and_symlinks(executable, py_executable, home_dir)
  File "/anaconda/lib/python3.6/site-packages/virtualenv.py", line 2828, in copy_exe_shared_libs_and_symlinks
    of = codefile(f, rpaths)
  File "/anaconda/lib/python3.6/site-packages/virtualenv.py", line 2799, in codefile
    return machofile(file, list(initial_rpaths_transitive))
  File "/anaconda/lib/python3.6/site-packages/virtualenv.py", line 2422, in __init__
    _, sos = zip(*mach_o_find_dylibs(file))
  File "/anaconda/lib/python3.6/site-packages/virtualenv.py", line 2386, in mach_o_find_dylibs
    do_file(ofile, find_lc_load_dylib, offset_size(), results, regex)
  File "/anaconda/lib/python3.6/site-packages/virtualenv.py", line 2355, in do_file
    do_file(file, offset_size(offset, size), *args)
TypeError: do_file() takes at least 4 arguments (3 given)

Since you already have Anaconda installed, you can create a Python virtual environment by executing the following command in the terminal- 由于您已经安装了Anaconda,您可以通过在终端中执行以下命令来创建Python虚拟环境 -

conda create -n pythonenvname python=x.x anaconda

Because you are trying to create a Python 2.7 virtual environment, I would suggest you using python27 as your environment name for convenience. 因为您正在尝试创建Python 2.7虚拟环境,所以我建议您使用python27作为环境名称以方便使用。 Moreover, xx refers to the version of Python that would be used to create the environment. 此外, xx指的是用于创建环境的Python版本。 In your case, it's 2.7 . 在你的情况下,它是2.7

So this is the command you should run to set up your virtual environment- 因此,这是您应该运行以设置虚拟环境的命令 -

conda create -n python27 python=2.7 anaconda

After you have installed the environment, run the following command to activate it- 安装环境后,运行以下命令将其激活 -

source activate python27

Now, if you run any python command, for example, python helloworld.py , then the Python 2.7 interpreter would be used. 现在,如果你运行任何python命令,例如python helloworld.py ,那么将使用Python 2.7解释器。

To deactivate the Python environment and return to your default one, simply execute the following command- 要停用Python环境并返回默认环境,只需执行以下命令 -

source deactivate

For further reference, check out this site . 如需进一步参考,请查看此站点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM