Skip to content

Latest commit

 

History

History
182 lines (116 loc) · 4.86 KB

File metadata and controls

182 lines (116 loc) · 4.86 KB

Python, virtual environments, VS Code, and Git

Table of contents


Python location
where python

python version
python --version or python -V

python run module (pip):
python -m pip


pip version

pip --version or pip -V

pip location

where pip

Upgrade pip

python -m pip install -U pip (short for) python -m pip install --upgrade pip

pip list

best practice: python -m pip install requests pip install requests

pip upgrade

pip install Django -U (short for) pip install Django --upgrade

alternatively python -m --upgrade Django

Create a requirements.txt file containing the names of your dependencies. You can use the pip command freeze for this task: pip freeze > requirements.txt

With pip, you can list all installed packages and their versions with pip freeze In most linux systems, you can pipe this to grep (or findstr on Windows) to find the row for the particular package you're interested in:

Linux:

$ pip freeze | grep lxml
lxml==2.3

Windows:

c:\> pip freeze | findstr lxml
lxml==2.3

pip uninstall Django

batch uninstall: pip uninstall -r uninstall.txt or python -m pip uninstall -r uninstall.txt -y


Create a virtual environment called [appenv] in the current folder
python -m venv appenv

This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named venv.

Activate the virtual environment
(Win)
venv\Scripts\activate

(Linux)
source venv\bin\activate

Install packages with pip
pip install requests

Deactivate virtual environment
deactivate

To delete a virtual environment, just delete its folder.
(In this case, it would be rm -rf venv.)

In order to keep your environment consistent, it’s a good idea to “freeze” the current state of the environment packages.
pip freeze > requirements.txt

To re-create the enviroment with the same packages and versions elsewhere
pip install -r requirements.txt

Lastly, remember to exclude the virtual environment folder from source control by adding it to the ignore list (see Version Control Ignores).
.ignore file:
\appenv


pip and virtualenv

pip3 always operates on the Python3 environment only, as pip2 does with Python2. pip operates on whichever environment is appropriate to the context. For example if you are in a Python3 venv, pip will operate on the Python3 environment.

Pip works as a module with -m, and so does venv for Python 3, so if you have Python 3.5 installed and stand in a project directory, you could e.g. do (assuming linux or MacOS):

python3.5 -m venv venv35

source venv35/bin/activate

(venv35) [Prompt] pip install <whatever>

That will install <whatever> in the Python3.5 virtual environment venv35 in your current directory.
Another example:

python -m venv env
source env/bin/activate
pip install -r requirements.txt

placeholder


Local Repository

Remote Repository


placeholder


Footnotes & Linkies