This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues/pull-requests.
notes/Terminal Tips/Commands + Settings/Languages/Python/tools/Environments/Intro to Python Environment...

111 lines
5.2 KiB
Markdown
Raw Permalink Normal View History

2023-07-05 03:05:42 +00:00
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](https://towardsdatascience.com/best-practices-for-setting-up-a-python-environment-d4af439846a) 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](https://realpython.com/python-virtual-environments-a-primer/) via Real Python
- how to set up a python environment in [VSCode](https://code.visualstudio.com/docs/python/environments?source=post_page...)
- This is a useful [shortcut](https://iambonface.medium.com/how-to-declutter-your-python-3-virtual-environments-from-possible-version-conflicts-d9eed4998775) 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 ](https://www.tjelvarolsson.com/blog/begginers-guide-creating-clean-python-development-environments/)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](obsidian://open?vault=Obsidian&file=Coding%20Tips%2FComputers%2FPython%2Ftools%2FEnvironments%2FVenv%2FVenv)
- [virtualenv](obsidian://open?vault=Obsidian&file=Coding%20Tips%2FComputers%2FLanguages%2FPython%2Ftools%2FEnvironments%2FVenv%2FVirtualenv)
- a really easy to set up [tutorial](https://iambonface.medium.com/how-to-declutter-your-python-3-virtual-environments-from-possible-version-conflicts-d9eed4998775) on virtualenv and virtualenvwrapper!
- [conda](obsidian://open?vault=Coding%20Tips&file=Computers%2FPython%2Ftools%2FEnvironments%2FConda)
---
##### [Treat Them as Disposables](https://realpython.com/python-virtual-environments-a-primer/#treat-them-as-disposables "Permanent link")
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 dont 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 shouldnt commit your virtual environment to [version control](https://realpython.com/python-git-github-intro/#version-control), and you shouldnt ship it with your project.
Because virtual environments arent entirely self-sufficient Python installations but rely on the base Pythons standard library, you wont create a portable application by distributing your virtual environment together with your code.
---