Virtual Environments with Python: Difference between revisions
No edit summary |
|||
| Line 12: | Line 12: | ||
First, install `virtualenv` if it is not already installed: `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> | First, install `virtualenv` if it is not already installed: `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=" | <syntaxhighlight lang="sh"> | ||
$ virtualenv -p /Library/Frameworks/Python.framework/Versions/3.4/bin/python3 venv | $ virtualenv -p /Library/Frameworks/Python.framework/Versions/3.4/bin/python3 venv | ||
$ source venv/bin/activate | $ source venv/bin/activate | ||
| Line 20: | Line 20: | ||
Once the virtualenv has been created it can be activated with the 2nd command: | Once the virtualenv has been created it can be activated with the 2nd command: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="sh"> | ||
$ source ~/venv/bin/activate | $ source ~/venv/bin/activate | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 30: | Line 30: | ||
To find the path to Python 3.x: | To find the path to Python 3.x: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="sh"> | ||
$ which python3 | $ which python3 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 20:53, 20 July 2018
Overview
Mac OS X Yosemite comes with Python 2.7 installed.
After installing Python 3.4, the python command still invokes Python 2.x. To use Python 3 use the python3 command instead.
Similarly, the pip command invokes pip that was installed with Python 2.x. So, 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. (pip3 is installed as part of the Python 3.x package.)
Creating virtual environments on Mac
First, install virtualenv if it is not already installed: 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 venv $ source venv/bin/activate $ pip install package-name
Once the virtualenv has been created it can be activated with the 2nd command:
$ source ~/venv/bin/activate
On Windows: .\venv\scripts\activate
To exit the virtualenv enter deactivate at the command prompt. This routine is defined within the virtualenv activate script.
To find the path to Python 3.x:
$ which python3
N.B. Packages will probably have to be installed for new virtual environments, e.g. django, etc.
Using a virtual environment in PyCharm
- PyCharm > Preferences > Project: '[CURRENT_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]
- Click the gear icon to the right of Project Interpreter:
References
- ↑ Build a
virtualenvof python 3 - Stack Overflow - ↑ Creating Virtual Environment - PyCharm 4.5.3 Help