- I would recommend skipping installing Python on your machine for now. Instead, I would start with something like Google Colab. It's linked to your Google account and Google Drive and they have a generous free tier. This is an excellent environment for first learning Python in and similar to a Jupyter Lab/Notebook environment that you will likely use in the future at some point.
- After using Google Colab for some time, Python can be installed multiple ways. My recommendations:
- Windows
- Scoop or Chocolatey package manager (
scoop install python
) - Anaconda/Spyder provides a GUI similar to Matlab's GUI (great for data science, data exploration, or other STEM tasks)
- Using Ubuntu under Window's Subsystem for Linux (then see Linux instructions).
- Scoop or Chocolatey package manager (
- macOS
- Homebrew package manager (
brew install python
) - Anaconda/Spyder provides a GUI similar to Matlab's GUI (great for data science, data exploration, or other STEM tasks)
- Homebrew package manager (
- Linux
- Distrobution's package manager (Debina/Ubuntu:
apt install python3 && apt install python3-pip
) - Anaconda/Spyder provides a GUI similar to Matlab's GUI (great for data science, data exploration, or other STEM tasks)
- Distrobution's package manager (Debina/Ubuntu:
- Windows
- Note: I'm using a *unix system containing Bash syntax as the example for these commands.
- Please, please, please use a virtual environment (abbreviated venv or virtualenv) for each of your projects! I can't stress this enough. Virtual environments are a way to isolate the dependencies of projects with respect to your system's environment. Think of it like global and local scope. Playing with global scope can be dangerous unless you know what you're doing. The virtual environment may look different depending on your installation method.
- System package manager, Python.org, or any other installation method: Python comes bundled with a module that helps you minimally manage your virtual environments. I'd recommend starting with this tool to learn the flow of creating environments, installing packages inside them, and removing them before before trying other tools that can manage some of those steps for you.
- A new Python virtual environment can be created with
python3 -m venv <path-to-environment-directory>
. This will create a Python virutual environment at the directory specified (thevenv
module will create the directory as long as the parent directory exists). - To activate the virtual environment, run
source <path-to-environment-directory>/bin/activate
. Runningwhich python3
should now give the location of the Python in the virtual environment (Note: the virtual environment will carry the version of whatever Python it was created with; therefore, runningpython
is synonymous withpython3
as ispip
andpip3
). The package installer, pip, is also a part of the environment (pip
is actually a Python module just likevenv
) and any packages install with it will be isolated inside the virtual environment. To say differently, any package installed with pip in the virtual environment will not be located by Python when the virtual environment is not active. - To deactivate the virtual environment, run
deactivate
. To delete the virtual environment, delete the directory specified when creating it. - After playing with the
venv
module, I'd recommend looking into thevirtualenv
package instead of using the nativevenv
module shipped with Python. It provides many niceties over thevenv
module and is one of the most popular. For me, the main difference is integration withpyenv
and the location of the environments it creates is automatically managed.
- A new Python virtual environment can be created with
- Anaconda/Miniconda: Anaconda and Miniconda come with a built in environment manager and Python package registry,
conda
. By default, an Anaconda install includes many data science packages (numpy
,pandas
,matplotlib
,jupyter
, etc.). Many Anaconda users don't need to mess with packages or environments for a while. Anaconda has good documentation for these topics and there is an excellent post by Gergely Szerovay. - Any installation method: Poetry is a Python dependency management tool that manages project environments and packages. It bundles environment management, package installation (with separation between development and package dependencies), and package publishing. Their website is the source of documentation as the project is continuously evolving.
- System package manager, Python.org, or any other installation method: Python comes bundled with a module that helps you minimally manage your virtual environments. I'd recommend starting with this tool to learn the flow of creating environments, installing packages inside them, and removing them before before trying other tools that can manage some of those steps for you.
- Don't understand something about the language?
- Start here: Python Docs Search
- Still don't understand? That's okay, try here: W3 Schools
- Want to ask a question? (make sure to always search online for an answer first!)
- Connect with hundreds of thousands of experienced and young Pythonistas like you: r/LearnPython
- Optimizing or solving a complex problem: StackOverflow Python
- What does idiomatic Python look like?
- Idiomatic Python code is referred to as "pythonic". The most popular python code syntax standard is released as a PEP (Python Enhancement Proposal). It is called PEP 8. It is generally referred to as the standard of how Python code should be written.
- There are many code syntax and format analyzers/checkers. Recently, most popular is
black
,autopep8
,isort
, andmypy
.black
is a code formatter. It is very opinionated, but generally accepted. Another alternative isautopep8
.isort
sorts your imports. This is not mentioned in PEP 8, but is generally preferred over random import lists.mypy
is a static type checker. As of Python 3.5, Python gained optional static typing features. Using a linter likemypy
can catch type errors that duck typing/dynamic typing languages often allow us to make.
- Exercism.io
- I'm actually a Python mentor on this site, its really easy to join and practice/learn good Python styling and code structure. Make sure to try mentored mode if you can. The feedback mentors give you can be invaluable.
- W3 Schools
- Provides quizzes with immediate feedback.
- Python Practices
- Provides a solution and difficulty levels.
- Python Data Science Handbook
- Includes Google Colab notebook links!
- Python isn't found as a command?
- This means Python isn't in the PATH. PATH is a list of the directories that your shell searches for when you run a command. Editing the PATH differs by OS. Please check with a guide specific to your OS.
- Why don't you recommend installing Python from python.org?
- The Python Organization's installation of Python doesn't give you the added benefits that installing by other means does. System package managers can help you keep your Python installation up to date and generally place python in your system's PATH. Anaconda comes with it's own environment and Python package manager (conda vs. pip). These save you the time of having to manually set up the PATH and keep your Python install updated (and save you the possibility of making a mistake while installing Python manually and having to venture into Google Hell to fix it). To be clear, there is nothing wrong with installing from python.org and sometimes there are reasons for it. But, for the average user, there are better methods in my opinion.
- What is a
requirements.txt
and why do I need one?- Many projects have a
requirements.txt
file that list the packages used in the Python code for that project. It's simply a list in the format of<package>==<version>
. You can install those packages using pip really easily! Trypip3 install -r requirements.txt
(the-r
flag is short for--requirement
). Make sure you're in a virtual environment first! It's helpful so that 3rd parties that use your code can understand and install packages that satisfy the requirements needed to run your code successfully. You yourself can create one from your Python environment by runningpip3 freeze > requirements.txt
.
- Many projects have a
- I see online that Python 2 has reached its end of life. What does that mean?
- Python, like all software, has versions. Python 2 has been around since the early 2000's and the Python Organization decided to bump the major version (2 -> 3) as there were many breaking changes between the versions (the versions are not closely compatible). Recently, Python 2 has been in maintenance mode and only security patches were released as new development on Python shifted to focus only on Python 3. As of 2020, Python 2 is no longer maintained and will not receive any patches or updates including for security. It is recommended that all new Python code be written so that it is compatible with Python 3.
- What are optional types in Python?
- Python 3.5 introduced the concept of [and 3.6 changed the syntax of] optional types. Optional types are type annotations in Python code to improve the readability, maintenance, and testing of Python code. I generally see this as a boon. Static typing gives users of a codebase more information (such as better hints on what types a function expects, etc.) on how to satisfy APIs and maintain existing APIs.
mypy
is one of the more popular static type checkers in Python. More info can be found in PEP 484.
- Python 3.5 introduced the concept of [and 3.6 changed the syntax of] optional types. Optional types are type annotations in Python code to improve the readability, maintenance, and testing of Python code. I generally see this as a boon. Static typing gives users of a codebase more information (such as better hints on what types a function expects, etc.) on how to satisfy APIs and maintain existing APIs.
- talk about [Ana/mini]conda
- talk about poetry
- separate out into repo