Virtual Environments with Python: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "Category:Python Category:Web Development == Overview == Mac OS X Yosemite comes with Python 2.7 installed. After installing Python 3.4, the `python` command still i...")
 
No edit summary
Line 10: Line 10:
== Creating virtual environments on Mac ==
== Creating virtual environments on Mac ==


First, install `virtualenv` if it is not already installed: `sudo pip(3) install virtualenv`. A new terminal shell is required to have the `virtualenv` command available.
First, install `virtualenv` if it is not already installed: `sudo pip(3) install virtualenv`. A new terminal shell is required to have the `virtualenv` command available.<ref>[http://stackoverflow.com/a/10763461 Build a `virtualenv` of python 3] - Stack Overflow</ref>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 23: Line 23:
$ which python3
$ which python3
</syntaxhighlight>
</syntaxhighlight>
== Using a virtual environment in PyCharm ==
* '''File''' > '''Default Settings...''' > '''Default Project''' > '''Project Interpreter'''
** Click the gear icon to the right of '''Project Interpreter:'''
*** Choose '''Add Local''' for existing virtualenv, or '''Create VirtualEnv''' for a new one
*** Select the virtualenv, and apply the changes.<ref>[https://www.jetbrains.com/pycharm/help/creating-virtual-environment.html Creating Virtual Environment] - PyCharm 4.5.3 Help</ref>
== References ==
<references />
* [http://hackercodex.com/guide/python-development-environment-on-mac-osx/#virtualenv Python Development Environment on Mac OS X Yosemite 10.10: virtualenv] - Hacker Codex

Revision as of 20:49, 28 August 2015

Overview

Mac OS X Yosemite comes with Python 2.7 installed.

After installing Python 3.4, the python command still invokes Python 2.x. This can be overcome by using python3 instead.

Similarly, the pip command invokes pip that was installed with Python 2.x. So, sudo pip install [package_name] will install for Python 2.x and leave Python 3.x unchanged. pip3 can be used to update Python 3.x on the command line.

Creating virtual environments on Mac

First, install virtualenv if it is not already installed: sudo pip(3) install virtualenv. A new terminal shell is required to have the virtualenv command available.[1]

$ virtualenv -p /Library/Frameworks/Python.framework/Versions/3.4/bin/python3 py3env
$ source py3env/bin/activate
$ pip install package-name

To find the path to Python 3.x:

$ which python3

Using a virtual environment in PyCharm

  • File > Default Settings... > Default Project > Project Interpreter
    • Click the gear icon to the right of Project Interpreter:
      • Choose Add Local for existing virtualenv, or Create VirtualEnv for a new one
      • Select the virtualenv, and apply the changes.[2]

References

  1. Build a virtualenv of python 3 - Stack Overflow
  2. Creating Virtual Environment - PyCharm 4.5.3 Help