Python location
where python
python version
python --version or python -V
python run module (pip):
python -m pip
pip --version or pip -V
where 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 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.3Windows:
c:\> pip freeze | findstr lxml
lxml==2.3pip 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
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.txtplaceholder
placeholder
- Python.org
- PyPA - The Python Packaging Authority
- pip latest version
- PyPI - the Python Package Index
- The Hitchhiker’s Guide to Python!
- Pipenv & Virtual Environments
- What is difference between pip and pip3? - Quora
- Better organization of your projects with python imports
- Creating the Perfect Python Dockerfile
- GitHub - Production tools for Data Science
- Design Patterns in Python- The Catalog of Python Examples
- Python SpeedSheet
- Objects, Memory, and Mutation in Python