Virtual Environments with Python: Difference between revisions
| (11 intermediate revisions by the same user not shown) | |||
| Line 6: | Line 6: | ||
After [[Installing Python 3 on Mac OS|installing Python 3.4]], the `python` command still invokes Python 2.x. To use Python 3 use the `python3` command instead. | After [[Installing Python 3 on Mac OS|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, ` | 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 == | == Creating virtual environments on Mac == | ||
=== Using python3 === | |||
Per the Python 3.8.x documentation, a virtual environment can be created with: <ref>[https://docs.python.org/3/library/venv.html venv - Creation of Virtual Environments] - Python Standard Library Documentation</ref> | |||
<pre> | |||
$ python3 -m venv /path/to/new/environment/ | |||
</pre> | |||
`venv` is the command that is run with python to create the virtual environment. | |||
=== Using virtualenv === | |||
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 | $ virtualenv -p /Library/Frameworks/Python.framework/Versions/3.4/bin/python3 venv | ||
$ source | $ source venv/bin/activate | ||
$ pip install package-name | $ pip install package-name | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Activating a virtual environment == | |||
An existing virtual environment is activated with: | |||
<syntaxhighlight lang=" | <syntaxhighlight lang="sh"> | ||
$ source ~/ | $ source ~/venv/bin/activate | ||
</syntaxhighlight> | </syntaxhighlight> | ||
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: | To find the path to Python 3.x: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="sh"> | ||
$ which python3 | $ which python3 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 35: | Line 52: | ||
== Using a virtual environment in PyCharm == | == Using a virtual environment in PyCharm == | ||
* ''' | * '''PyCharm''' > '''Preferences''' > '''Project: '[CURRENT_PROJECT]'''' > '''Project Interpreter''' | ||
** Click the gear icon to the right of '''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 | *** 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> | *** 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 == | == Deactivating a virtual environment == | ||
<syntaxhighlight lang="sh"> | |||
$ deactivate | |||
</syntaxhighlight> | |||
== Updating the python version for an existing virtual environment == | |||
First, deactivate the virtual environment if it is running. <ref>[https://stackoverflow.com/a/51915509 How to change the python version of already existing virtualenv?] - Stackoverflow</ref> | |||
<syntaxhighlight lang="sh"> | |||
$ virtualenv --clear -p /path/to/my/python3.6 ./venv | |||
</syntaxhighlight> | |||
Confirm the python version used by the virtual environment with: | |||
<syntaxhighlight lang="sh"> | |||
$ ls -la ./venv/bin/python | |||
</syntaxhighlight> | |||
== Notes == | |||
=== See also === | |||
* [https://docs.python-guide.org/dev/virtualenvs/ Pipenv & Virtual Environments] - The Hitchhiker's Guide to Python | |||
* [http://hackercodex.com/guide/python-development-environment-on-mac-osx/#virtualenv Python Development Environment on Mac OS X Yosemite 10.10: virtualenv] - Hacker Codex | |||
=== References === | |||
<references /> | <references /> | ||
Latest revision as of 00:01, 20 March 2020
Overview[edit]
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[edit]
Using python3[edit]
Per the Python 3.8.x documentation, a virtual environment can be created with: [1]
$ python3 -m venv /path/to/new/environment/
venv is the command that is run with python to create the virtual environment.
Using virtualenv[edit]
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.[2]
$ virtualenv -p /Library/Frameworks/Python.framework/Versions/3.4/bin/python3 venv $ source venv/bin/activate $ pip install package-name
Activating a virtual environment[edit]
An existing virtual environment is activated with:
$ 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[edit]
- 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.[3]
- Click the gear icon to the right of Project Interpreter:
Deactivating a virtual environment[edit]
$ deactivate
Updating the python version for an existing virtual environment[edit]
First, deactivate the virtual environment if it is running. [4]
$ virtualenv --clear -p /path/to/my/python3.6 ./venv
Confirm the python version used by the virtual environment with:
$ ls -la ./venv/bin/python
Notes[edit]
See also[edit]
- Pipenv & Virtual Environments - The Hitchhiker's Guide to Python
- Python Development Environment on Mac OS X Yosemite 10.10: virtualenv - Hacker Codex
References[edit]
- ↑ venv - Creation of Virtual Environments - Python Standard Library Documentation
- ↑ Build a
virtualenvof python 3 - Stack Overflow - ↑ Creating Virtual Environment - PyCharm 4.5.3 Help
- ↑ How to change the python version of already existing virtualenv? - Stackoverflow