Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create virtual env with python3

I am using python 2.7 + virtualenv version 1.10.1 for running myproject projects. Due to some other projects requirement I have to work with other version of python(Python 3.5) and Django 1.9. For this I have installed python in my user directory. Also I have dowloaded and installed virtualenv( version - 15.1.0) into my user directory. But whenever I am trying to create virtual env I am getting the below error

python virtualenv/virtualenv.py myproject

Using base prefix '/home/myuser/python3'
New python executable in /home/mount/myuser/project_python3/myproject/bin/python
ERROR: The executable /home/mount/myuser/project_python3/myproject/bin/python is not functioning
ERROR: It thinks sys.prefix is '/home/myuser/python3' (should be '/home/mount/myuser/project_python3/myproject')
ERROR: virtualenv is not compatible with this system or executable

Can anybody tell what I am doing wrong with this

like image 699
Anish Avatar asked Mar 28 '17 12:03

Anish


People also ask

What is python3 virtual environment?

Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories that doesn't share libraries with other virtualenv environments (and optionally doesn't access the globally installed libraries either).


3 Answers

In Python 3.6+, the pyvenv module is deprecated. Use the following one-liner instead:

python3 -m venv <myenvname>

This is the recommended way to create virtual environments by the Python community.

like image 196
The Aelfinn Avatar answered Oct 09 '22 04:10

The Aelfinn


To create virtual env

virtualenv -p python3 venv_name 

This will create new python executable in baseDirectory/bin/python3

How to activate newely created Venv:

cd baseDirectory/bin/  

source activate  

Deactivate new venv

deactivate 

UPDATE_1

This method has been depreciated as The use of venv is now recommended for creating virtual environments. Please check this link for updated answer

like image 25
cryptoKTM Avatar answered Oct 09 '22 06:10

cryptoKTM


Python already ships with its builtin "virtualenv" called venv since version 3.3. You no longer need to install or download the virtualenv scripts for Python 3.3+.

https://docs.python.org/3/library/venv.html

Check that your installation provided the pyvenv command that should take care of creating the "virtualenv". Arguments are similar to the classic virtualenv project.

$ pyvenv --help
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
            [--upgrade] [--without-pip]
            ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
  ENV_DIR               A directory to create the environment in.

optional arguments:
  -h, --help            show this help message and exit
  --system-site-packages
                        Give the virtual environment access to the system
                        site-packages dir.
  --symlinks            Try to use symlinks rather than copies, when symlinks
                        are not the default for the platform.
  --copies              Try to use copies rather than symlinks, even when
                        symlinks are the default for the platform.
  --clear               Delete the contents of the environment directory if it
                        already exists, before environment creation.
  --upgrade             Upgrade the environment directory to use this version
                        of Python, assuming Python has been upgraded in-place.
  --without-pip         Skips installing or upgrading pip in the virtual
                        environment (pip is bootstrapped by default)

Once an environment has been created, you may wish to activate it, e.g. by
sourcing an activate script in its bin directory.
like image 6
user73657 Avatar answered Oct 09 '22 06:10

user73657