36

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

Anish
  • 3,929
  • 6
  • 31
  • 55
  • 1
    It's not clear which Python you are using when you run `python virtualenv/virtualenv.py myproject`. Is `python` here your system Python (2.7) or your local Python (3.5)? – Chris Mar 28 '17 at 12:38
  • 1
    Did you use the correct pip, i.e. pip3, to install virtualenv version - 15.1.0? – PM 2Ring Mar 28 '17 at 12:43
  • I always find [these docs](http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/) helpful. – blacksite Mar 28 '17 at 12:56
  • @Chris I am using python3.5 – Anish Mar 28 '17 at 13:19
  • @PM2Ring I have used the command python -m pip – Anish Mar 28 '17 at 13:21
  • Generally, `python` means Python 2, you need to use `python3` to refer to Python 3. Eg, `python3 -m pip install some_package`. But as others have said, it's better to use the `venv` module that is included in recent versions of Python 3. – PM 2Ring Mar 28 '17 at 13:45
  • @PM2Ring I have updated my bashrc_profile with path . That' the reason I am referring python :). BTW I have installed the python 3.5.3 manually – Anish Mar 28 '17 at 13:52
  • Hmmm. That may not be wise if you have stuff that expects `python` to refer to Python 2, eg Python 2 scripts that aren't Python 3 compatible and which have a `#!/usr/bin/env python` shebang line. A safer way is to make `python` an alias for `python3`. – PM 2Ring Mar 28 '17 at 14:12
  • @PM2Ring . Thanks for you suggestion. I have created an alias for python3 in my bashrc_profile – Anish Mar 30 '17 at 04:08
  • See also https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe Currently the number of "recommended methods" of creating virtual env's in python continues to increase; see also: (1) ["Pipenv is the officially recommended Python packaging tool from Python.org"](https://docs.pipenv.org/) and (2) http://docs.python-guide.org/en/latest/dev/virtualenvs/ and (3) [xkcd: Standards](https://xkcd.com/927/) – michael Nov 24 '17 at 23:25

8 Answers8

81

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.

The Aelfinn
  • 9,770
  • 1
  • 43
  • 43
8

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 
cryptoKTM
  • 1,588
  • 14
  • 17
  • Worth to remember is what people are saying below: since Python version 3.3, there is no need to install and use `virtualenv`, as `venv` exists as an embedded module. Btw. creating alias for `source venv/bin/activate` is much more efficient than cd and activating it. – pdaawr May 25 '21 at 12:11
6

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.
user73657
  • 390
  • 2
  • 9
1

Since the launch of Python version 3.3, there has been no need to download the virtualenv package separately as it comes built-in in Python.

Refer to the documentation to gain complete insights on it.

Test the installation of virtualenv:

$ virtualenv --version 

Usage:

1.Creating a virtual environment:
$ virtualenv --system-site-packages -p python3 ./virtual_env_name

2.For enabling it, use the following command:
$ source ./virtual_env_name/bin/activate

3.For disabling the virtual environment and get back to working with the local environment:
$ deactivate

For listing down the packages in the virtual environment, use the following command:
$ pip3 list

davidjb
  • 7,069
  • 3
  • 27
  • 41
Yogita Bhatia
  • 121
  • 1
  • 2
0

I install it using the command (for Python 3.x),

$ python3 -m venv env
Arefe
  • 6,963
  • 10
  • 60
  • 110
0

To create a virtual environment in python3:

virtualenv -p /usr/bin/python3 virtualenvname

After creating the virtual environment, we need to activate it using the below command:

source virtualenvname/bin/activate

to deactivate use the below command:

deactivate

OM Bharatiya
  • 707
  • 8
  • 15
VIJAYA SRI
  • 22
  • 3
0

virtualenv is the tool of choice for Python 2, while venv handles the task in Python 3.

Yet you can create the virtual environment for Python 3 using any of them.

Using venv

python3 -m venv virtualenvname

Command Syntax:

/path/to/python3 -m venv /path/to/directory/virtual_env_name

Using virtualenv

virtualenv -p python3 virtualenvname

Command Syntax:

virtualenv -p /path/to/python3 /path/to/directory/virtual_env_name

Activate the virtual environment

On Linux, Unix or MacOS, using the terminal or bash shell:

source /path/to/venv/bin/activate

e.g. source virtualenvname/bin/activate

On Unix or MacOS, using the csh shell:

source /path/to/venv/bin/activate.csh

On Unix or MacOS, using the fish shell:

source /path/to/venv/bin/activate.fish

On Windows using the Command Prompt:

path\to\venv\Scripts\activate.bat

On Windows using PowerShell:

path\to\venv\Scripts\Activate.ps1

Deactivating the virtual environment

On Linux, Unix or MacOS, using the terminal or bash shell:

deactivate

On Windows using the Command Prompt:

path\to\venv\Scripts\deactivate.bat

On Windows using PowerShell:

deactivate

This answer is for those who may use a different OS.

OM Bharatiya
  • 707
  • 8
  • 15
-1

Install virtualenvwrapper on top of virtualenv to simplify things. Follow the blog to install in easy steps: virtualenvwrapper

Steps to create it:

  1. mkvirtualenv -p /usr/bin/python3
  2. Install packages using - pip install package_name
  3. workon - activates the virtualenv, deactivate - deactivates the viirtualenv
Henin RK
  • 282
  • 2
  • 14
  • 1
    How will adding a wrapper around `virtualenv` help if `virtualenv` itself isn't working? – Chris Mar 28 '17 at 13:39
  • This is a repeated answer. This is also an incorrect answer. virtualenv is deprecated, this is a wrapper around virtualenv – The Aelfinn Mar 28 '17 at 14:10