151

I am using Python 2.7 + virtualenv version 1.10.1 for running myproject project. Due to some other projects requirement I have to work with another version of Python (3.5). For this I have installed Python in my user directory. Also I have downloaded 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 

What am I doing wrong here and how do I solve it?

10
  • 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)? Commented Mar 28, 2017 at 12:38
  • 1
    Did you use the correct pip, i.e. pip3, to install virtualenv version - 15.1.0? Commented Mar 28, 2017 at 12:43
  • I always find these docs helpful. Commented Mar 28, 2017 at 12:56
  • @Chris I am using python3.5 Commented Mar 28, 2017 at 13:19
  • @PM2Ring I have used the command python -m pip <virtualenv package> Commented Mar 28, 2017 at 13:21

13 Answers 13

276

Use:

python3 -m venv ./path-to-new-venv 

This is the recommended way to create virtual environments.

Historically, a wrapper command pyvenv was provided for this. However, the wrapper was deprecated in Python 3.6, and removed in 3.8.

Sign up to request clarification or add additional context in comments.

1 Comment

word of warning.. This did not do what I expected it to do, which was to create a virtual environment called myenvname in some standard location. Instead, it created a directory called ./myenvname/, and put a bunch of files in it. Since I already had a directory with that name, those files are all mixed up with what I had before. I think better is python3 -m venv ~/path/to/where/you/want/it (I recognize it was naive of me to expect it to do something without reading the docs first)
83

To create a virtual environment, go to your project’s directory and run the following command. This will create a new virtual environment in a local folder named .venv:

python3 -m venv .venv

Activate a virtual environment

source .venv/bin/activate

To confirm the virtual environment is activated, check the location of your Python interpreter:

which python

To deactivate a virtual environment

deactivate

See more here: https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/

1 Comment

Since the question doesn't specify an operating system, it should be noted that the activation script might also be in .venv/Scripts/activate
41

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

1 Comment

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.
13

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.

Comments

8

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. 

3 Comments

This script is deprecated in Python 3.6+, although the module still exists. The recommended use is: python3 -m venv <myenvname>
@user73657 I am using python 3.5 . But if i run the command pyenv its saying command not found
@user73657 pyenv is not available in 3.5.3
4

Run these 2 commands -

python3 -m venv .venv source .venv/bin/activate 

Your virtual env is ready to use. In order to check this use -

which python 

Comments

2

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

$ python3 -m venv env 

Comments

2

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

Comments

2

CREATE VIRTUAL ENVIRONMENT:

For Python 3 version:

Command: python3 -m venv [environment_name]

Example: python3 -m venv my_virtual_environment

For Python 2 version:

Command: python -m [environment_name]

Example: python -m venv my_virtual_environment

ACTIVATE VIRTUAL ENVIRONMENT IN LINUX:

Go to the virtual environment directory then open terminal.

Command: source [environment_name]/bin/activate

Example: source my_virtual_environment/bin/activate

ACTIVATE VIRTUAL ENVIRONMENT IN WINDOWS:

Go to the virtual environment directory then open cmd.

Command: [environment_name]\Scripts\activate

Example: my_virtual_environment\Scripts\activate

Comments

1

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

Comments

1

If you are on windows.

  1. manually download and install the version of python you want from the official site

  2. after installation, search "python" to locate the folder, so you can identify the path

  3. get the path of the .exe (for example: C:\Users\Path\Programs\Python\Python38\python.exe)

  4. Inside the folder of which you want to create the environment...start bash or VSCode terminal, or whatever command prompt, type [python .exe path] -m venv [env name] like this:

    C:/Users/Path/Programs/Python/Python38/python.exe -m venv myenv

  5. (Note that you have to change forward slash to backward slash for the path on step 4)

  6. Activate the environment like so:

    source myenv/Scripts/activate

Comments

0

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

2 Comments

How will adding a wrapper around virtualenv help if virtualenv itself isn't working?
This is a repeated answer. This is also an incorrect answer. virtualenv is deprecated, this is a wrapper around virtualenv
0

Try this batch script in widows to create your project, call it new-py-env.bat.

new-py-env.bat

mkdir %1 cd %1 py -m venv env env\Scripts\activate.bat 

how to use: ?

the script will accept the first parameter as the folder/directory name, so you can call it as.

new-py-env myproject01 from cmd.exe and it will create the project folder myproject01 then create a virtual environment and activates it.

what it does: ?

It encapsulates 4 operation

  1. Create the project folder with parameter %1
  2. Change the directory to %1
  3. Create a virtual environment named env
  4. Activates the environment env

The same can be done with linux with few changes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.