python venv pipenv pipx - ghdrako/doc_snipets GitHub Wiki

The module venv has a drawback: by default, it uses the systemwide Python. If you want to use a specific version of Python for your project, you should use other virtual environment management tools, such as conda .

mkdir taskier-app
cd taskier-app
python3 -m venv taskier-env
# for Mac:
$ source taskier-env/bin/activate
# for Windows:
> taskier-env\Scripts\activate.bat
pip install streamlit==1.10.0

Open the project directory ( taskier_app ) in VSC; press Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows) to display the command menu; and enter Python: Select Interpreter, which brings up the list of available virtual environments. You should be able to see the virtual environment taskier-env in the list. Select the ' taskier-env ': venv option

C:\Projects\python\venv> python -m venv <venv name>
C:\Projects\python\proj> mkdir <venv name>
C:\Projects\python\proj> cd <venv name>
C:\Projects\python\proj<venv name>>code .

To create a virtual environment, write down the following command, where “.venv” is the name of the environment folder:

# macOS and Linux
# You might need to run sudo apt-get install python3-venv first
python3 -m venv .venv
# Windows
# You can also use py -3 -m venv .venv
python -m venv .venv
python -m venv $VENV_PATH
source $VENV_PATH/bin/activate
pip install ...
pip freeze > requirements.txt
deactivate
  • Windows:
"python.pythonPath": "c:/dev/ala/venv/Scripts/python.exe",

Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P)

Opting for an interpreter sets the python.pythonPath value in your workspace settings to the path of the interpreter. To see that setting, select File ➤ Preferences ➤ Settings (Code ➤ Preferences ➤ Settings for macOS), then click on the Workspace Settings tab

py -3 -m venv .venv
.venv\scripts\activate
python -m pip install matplotlib

Python: Select Interpreter command and run the Terminal: Create New Integrated Terminal command (Ctrl+Shift+`)) to open a terminal with that environment activated. In the same terminal, run

pip freeze > requirements.txt 

to create the requirements.txt file in your project folder.

# windows
.\env\Scripts\activate
where python
...\env\Scripts\python.exe
# linux
source env/bin/activate
which python
deactivate
pip install -r requirements.txt

for Python 3, on Unix-like systems it’s:

pyvenv <directory_name>

and for Python 3 on Windows:

pyvenv.py <directory_name>

The virtual environment has its own site-packages directory, but shares the standard library and whatever Python binary you pointed it at during creation.

This creates a new directory with everything the virtual environment needs: lib (Lib on Windows) and include subdirectories for supporting library files, and a bin subdirectory (Scripts on Windows) with scripts to manage the virtual environment and a symbolic link to the appropriate Python binary. It also installs the pip and setuptools modules in the virtual environment so that you can easily install additional packages. Once the virtual environment has been created, you’ll need to navigate into that directory and “activate” the virtual environment by running a small shell script. This script tweaks the environment variables necessary to use the virtual environment’s Python and site-packages. If you use the Bash shell, you’ll run:

source bin/activate

Windows users will run:

Scripts\activate.bat

When you are done working in that virtual environment, the deactivate command will revert to using the default Python again.


pipenv

After installing via package manager (homebrew, apt, dnf, etc.) or pip installing into an existing Python environment (recommended to install as a user-level utility, as with virtualenv), we can create a new pipenv project in our project directory with

$ pipenv --python $PYTHON_VERSION

which will initialize the project using the specified Python version

o start with, this creates:

a Pipfile config file at the project home specifying Python version, sources, and any installed packages
a fresh virtual environment housed in the pipenv working directory

To install a package, simply running

$ pipenv install $PACKAGE_NAME

will both install the package into the virtual environment, and write the package as a dependency into the Pipfile. This Pipfile is then all we need to rebuild the project elsewhere, rather than the requirements.txt used by other managers - simply running pipenv install on a directory with a Pipfile will recreate the environment. To activate the environment,

$ pipenv shell

Next, pipenv can do something fairly unique - it fully determines and specifies dependencies for the project. pipenv exhaustively builds out the dependency graph, flagging any issues and generating a validated Pipfile.lock for fully specifying every dependency in the project. We can trigger this manually for the requirements in our Pipfile with

$ pipenv lock

to pull the specifically requested packages from the Pipfile and generate the dependency graph for Pipfile.lock. While this does produce environments that can be deterministically reproduced, the dependency resolution can be quite complex, so pipenv environments are slower to write than using bare pip. will launch a new shell process using the project’s virtual environment.

$ pip install --user pipenv
$ cd project_folder
$ pipenv shell # create a virtual environment if one doesn’t already exist. Pipenv creates all your virtual environments in a default location.
               # PIPENV_VENV_IN_PROJECT — If set, use .venv in your project directory instead of the global virtualenv manager pew.
$ pipenv install requests
$ pipenv run python main.py

pipx

pipx instal from PyPI but can also install from all other sources pip can, such as a local directory, wheel, git url, etc.

pipx install PACKAGE
pipx list

This automatically creates a virtual environment, installs the package, and adds the package's associated applications (entry points) to a location on your PATH. For example, pipx install pycowsay makes the pycowsay command available globally, but sandboxes the pycowsay package in its own virtual environment.

pipx run APP [ARGS...]

downloads and runs the above mentioned Python "apps" in a one-time, temporary environment, leaving your system untouched afterwards.

⚠️ **GitHub.com Fallback** ⚠️