5.2 KiB
Ultimately environments are just folder structures. But as with everything, programmers can make them seem real complicated. The power of the imagination!
>> which python
Gives you the current environment for python for that folder.
As listed in this best practices article, the way to do things now with python environments is to pyenv
--> pipX
--> Poetry
1. PyEnv
- list all available python versions:
# List all available python versions
>pyenv install -l
:: [Info] :: Mirror: [https://www.python.org/ftp/python](https://www.python.org/ftp/python)
2.4-win32
2.4.1-win32
2.4.2-win32
…
3.9.0a3
3.9.0a4-win32
3.9.0a4# Install a specific python version
>pyenv install 3.9.0a4# Rehash your new installables. Needed to update pyenv shims.
>pyenv rehash
- Global vs Local python versions
- check your global python version:
# Set global python version
>pyenv global 3.9.0a4# Check global python version
>pyenv global
3.9.0a4
- set your local python version within a specific project folder:
# Install other verion of python
>pyenv install 3.8.2# Change directory to your project
>cd DreamProject# Set local python version inside your project
DreamProject>pyenv local 3.8.2# Check your local python version
DreamProject>pyenv local
3.8.2
PipX
The global python package installer. Keep this dry and don't overcrowd, as you can always do that within your python virtual environments itself.
For example, to install black
formatter:
# Verify global python version is active
>pyenv global
3.9.0a4# Install pipx
>pip install pipx# Make sure pipx is added to the path
>pipx ensurepath# Install black globally
>pipx install black# Check if install is successful
>black --version
black, version 20.8b1
3. Poetry
The must-have to resolve dependency issues. Come back to update this later because you have to be finishing up your paper now.
- initiation Poetry:
# Create a directory and setup python version
DreamProject>pyenv local 3.8.2# Initiate poetry. This will ask meta info related to the project. DreamProject>poetry init
This command will guide you through creating your pyproject.toml config.Package name [DreamProject]:
Version [0.1.0]:
Description []:
Author [aspiring_dev <[aspiring_dev@gmail.com](mailto:mak.adi55@gmail.com)>, n to skip]:
License []:
Compatible Python versions [^3.8]:
more about Poetry found here
Some useful references:
- as a forever reference, here is a reference on all python environments via Real Python
- how to set up a python environment in VSCode
- This is a useful shortcut on how to declutter your python virtual environments.
Last word, the basics:
For virtual environments:
Python files tend to get pretty dang big nowadays, especially with all of these data science and machine learning projects. So the solution from the Python team was to create a python virtual environment. What's more, there's often library issues if you don't have the right python imports set up so it eventually becomes necessary to create one. To be honest the virtual environment has been a system since the conception of the computer, but python gives them different names here, namely:
- venv
- virtualenv
- a really easy to set up tutorial on virtualenv and virtualenvwrapper!
- conda
Treat Them as Disposables
Virtual environments are disposable folder structures that you should be able to safely delete and re-create at any time without losing information about your code project.
This means that you generally don’t put any additional code or information into your virtual environment manually. Anything that goes in there should be handled by your package manager, which will usually be pip
or conda
.
You also shouldn’t commit your virtual environment to version control, and you shouldn’t ship it with your project.
Because virtual environments aren’t entirely self-sufficient Python installations but rely on the base Python’s standard library, you won’t create a portable application by distributing your virtual environment together with your code.