2

I'm having a difficult time wrapping my head around how to utilize virtualenv and python3 together. As I understand it, virtualenv acts as an operating system within my mac's operating system. I installed virtualenv through the terminal and can activate/deactivate it successfully, but how do I use python3 with it?

I understand the python shell, I understand the terminal, but after I created the my_projects directory for virtualenv, how can I ensure I'm creating something in a virtualenv with python?

I'm not using homebrew or anaconda.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • Possible duplicate of [Using Python 3 in virtualenv](http://stackoverflow.com/questions/23842713/using-python-3-in-virtualenv) – Nir Alfasi Feb 09 '17 at 18:37
  • "As I understand it, virtualenv acts as an operating system within my mac's operating system" No. It acts as an *environment* isolated from your working environment. A virtual environment, you might say... – juanpa.arrivillaga Feb 09 '17 at 18:51

2 Answers2

1

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them.

It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.

For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8.

For more understanding refer to this Python Guide.

ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
Anurag Misra
  • 1,516
  • 18
  • 24
0

the virtual environment will isolate from the OS python. You can create a virtual env per project. For example project projectA, you can create an venv inside projectA as:

cd projectA
virtualenv -p /usr/bin/python3.5 venv-name-A

When you install any packages for projectA, you do: /path/to/venv-name-A/bin/pip install <pkg-name>

When you run your projectA, you do: /path/to/venv-name-A/bin/python projectA-file.py

You can create as many venvs as you want. You can install any packages on any envs without breaking your OS python accidentally.

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
  • Thank you, Haifeng. I'm struggling now because the path that my project was installed looks a bit different than yours. My looks like: /Users/Main/Desktop/Python_practice/Lamba/myproject. From the myproject directory I entered virtualenv venv. Did I install the virtual environment in the wrong location initially? – basiclanguage Feb 09 '17 at 18:59
  • @basiclanguage no matter where you store the venv, it is isolated from your OS python, and you know the installed packages are for the specific project:) – Haifeng Zhang Feb 09 '17 at 19:03
  • Okay cool, what would be the difference between these two: python3 -m pip install virtualenv & virtualenv -p /usr/bin/python3.5 venv-name-A ? – basiclanguage Feb 09 '17 at 19:17
  • i think it should be `pip install virtualenv`, in which installs virtulenv package. the latter command is creating venv based on python3.5 – Haifeng Zhang Feb 09 '17 at 19:21
  • This really clears it up for me, Haifeng. Thanks again. – basiclanguage Feb 09 '17 at 19:43