简体   繁体   中英

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. I have installed both python2.7 and python3.6 on the system. And run below code to setup 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. How can I make it to use python2.7 environment?

$ 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-

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. Moreover, xx refers to the version of Python that would be used to create the environment. In your case, it's 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.

To deactivate the Python environment and return to your default one, simply execute the following command-

source deactivate

For further reference, check out this site .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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