01 Things Must be
Considered
Q: I learned how to do python already, why should I learn “practical python
programming?”
A: Knowing python “language” != knowing python “application development”, we
will focus on the latter.
Key Points
● Modularization
● Dependency Management
● Good IDE
Modularization
● usually, pip install package
● why you need a package?
○ To effectively share your code with others.
○ They save you time. Even if you don’t intend to share your package with others, they help you
easily reuse and maintain your code across multiple projects.
○ They force you to organize and document your code, such that it can be easily understood
and used at a later time.
○ They isolate dependencies for your code and improve its reproducibility.
○ They are a good way to practice writing good code.
○ Packages can be used to effectively bundle up reproducible data analysis and programming
projects.
○ Finally, developing and distributing packages supports the Python ecosystem and other
Python users who can benefit from your work.
Example
Assume you have the directory structure below:
this is a moule
And assume SayHello.py is a very important file containing superb say hello function.
The SayHello.py module contains the following functions:
def simple():
print("Hello!")
def withName(name):
print("Hello, " + name)
Then, you can use the functions in the following way:
import utils.simple.SayHello as SayHello
SayHello.simple()
SayHello.withName("lendle")
Why Not
Why not?
def simple():
print("Hello!")
def withName(name):
print("Hello, " + name)
simple();
withName("lendle");
Dependency Management
usually, you use
pip install xxx
to install packages, don’t do that anymore!
What does pip install Do?
● First pip needs to decide which distribution of the package to install.
● Once pip has a list of compatible distributions, it sorts them by version,
chooses the most recent version, and then chooses the "best" distribution for
that version.
● Then it needs to determine which library directory to install the package
in—the system's, the user's, or a virtualenv's?
● Now your package is installed!
https://dev.to/alexbecker/how-pip-in
stall-works-323j
why it is scary to use pip install into system or user directory?
● Dependency Conflicts: System packages and other applications might rely on
specific versions of libraries. Installing a new package or upgrading an
existing one could lead to version conflicts, breaking system tools or other
applications.
● System Stability: Modifying the system-wide Python environment can impact
the stability and reliability of your system. Some system tools and scripts
depend on the default versions of libraries, and changing these can lead to
unpredictable behavior or system failures.
so you need virtualenv, but it is not recommended to use it in a project, why?
what does virtualenv do?
● Creates a Directory: When you run virtualenv <env_name>, it creates a directory
(usually named <env_name>) where the environment will reside.
● Copies Python Interpreter: It copies the Python interpreter binary into the new
directory.
● Installs pip: It installs a local instance of pip within the environment, allowing you to
install packages locally without affecting the global Python environment.
● Sets Up Environment Variables: It sets up environment variables such that when the
virtual environment is activated, Python and pip commands point to the local copies
within the environment.
Intro to Poetry
Poetry is a Python utility for dependency management, packaging, and publishing.
It covers all bases of managing a Python project with ease through its CLI and a
single configuration file.
You may use poetry CLI to:
● Installing and Managing Dependencies
● Configuring your packages
● Executing Applications
https://pythonistaplanet.com/poetry/
● Building and Publishing #:~:text=Python%20Poetry%20Tuto
rial%201%20Installation%20Before
%20we%20begin,Building%20and
%20Publishing%20...%208%20Fin
al%20Thoughts%20
● poetry init
● poetry add requests
● poetry install
● poetry update
● poetry run [script-name]
Installation
warning!
Poetry should always be installed in a dedicated virtual environment to isolate it
from the rest of your system.
Use Official Installer
Linux, macOS, Windows (WSL)
● curl -sSL https://install.python-poetry.org | python3 -
● curl -sSL https://install.python-poetry.org | python -
Windows (Powershell)
● (Invoke-WebRequest -Uri https://install.python-poetry.org
-UseBasicParsing).Content | py -
The installer creates a poetry wrapper in a well-known, platform-specific directory:
● $HOME/.local/bin on Unix.
● %APPDATA%\Python\Scripts on Windows.
● $POETRY_HOME/bin if $POETRY_HOME is set.
Add poetry to PATH.
Basic usage
First, setup a project:
poetry new poetry-demo
pyproject.toml
cd poetry-demo
poetry add cowsay
create test.py in the root directory of your project
import cowsay
print(cowsay.cow("hello"))
execute use poetry
poetry run python test.py
Good IDE
poetry is good
but too many command to remember!
you need to good IDE to mannage all this
the community
edition is free
and is enough
Advantages of Pycharm:
● Intelligent Code Editor
● Availability of Integration Tools
● Integrated Debugging and Testing
● Project and Code Navigation
● Refactoring
C:\ProgramData\anaconda3\python
.exe
C:\Users\user\AppData\Roaming\P
ython\Scripts\poetry.exe