overriding pyenv python versions, if you want to use a python version located on a different folder, i.e. /usr/local/bin/python3
on a new project, then:
mkdir <new_project>
cd <new_project>
<on new_project> mkdir venv
<on new_project> /usr/local/bin/python3 -m venv venv
This how-to uses the native venv. Goodbye to pipenv. pyenv still used. Tested as my current workflow on Mac OS. Linux should be happy, too.
- Choose your Python version
- From available local versions -
~/dev $ pyenv versions
- Or from the Internet, if no local version exists -
~/dev $ pyenv install -l
~/dev $ pyenv install 3.7.4
- Set the version as system default
~/dev $ pyenv global 3.7.4
- Set up the project folder
~/dev $ mkdir project-x
~/dev $ cd project-x
~/dev/project-x $ mkdir venv
- Create the virtual environment
~/dev/project-x $ $(pyenv which python3) -m venv venv
- Revert the system default Python version, if needed
~/dev/project-x $ pyenv global 3.6.8
- Activate the virtual environment
~/dev/project-x $ source venv/bin/activate
- Upgrade pip, if needed
(venv) ~/dev/project-x $ pip list
(venv) ~/dev/project-x $ pip install --upgrade pip
-
Work on the other Python packages, start-up VS Code, etc.
-
On VS Code, create a helloworld Python file to trigger VS Code asking for a Python interpreter; choose the appropriate venv and Python version. I also install pylint and black (via call to Format Document). Sample pip list output of the base install -
(venv) ~/dev/project-x $ pip list
Package Version
----------------- -------
appdirs 1.4.3
astroid 2.2.5
attrs 19.1.0
black 19.3b0
Click 7.0
isort 4.3.21
lazy-object-proxy 1.4.2
mccabe 0.6.1
pip 19.2.3
pylint 2.3.1
setuptools 40.6.2
six 1.12.0
toml 0.10.0
typed-ast 1.4.0
wrapt 1.11.2
(venv) ~/dev/project-x $
How to use linting: Using pylint in VS-Code
- Before closing a working day's session, deactivate the virtual
(venv) ~/dev/project-x $ deactivate
~/dev/project-x $
- to activate
~/dev/project-x $ source venv/bin/activate
- to deactivate
(venv) ~/dev/project-x $ deactivate
Be sure to pip freeze > requirements first.
pyenv uninstall <name of pyenv virtual env>
Proceed to create the venv virtualenv.
Restore Python package dependencies
pip install -r requirements.txt
Create a requirements from the pipenv lock output (copy-paste the output to requirements.txt) and remove the vm
pipenv lock -r
pipenv uninstall
pipenv --rm
Proceed to create the venv virtualenv.
Restore Python package dependencies
pip install -r requirements.txt
In Homebrew:
$ brew update && brew upgrade pyenv
Update pyenv:
$ pyenv update