Import Local Scripts
Import Local Scripts¶
Import is helpful when working on bigger projects where you want to organize the code into multiple files and reuse the code in those files, if the python script is in the same directory of the current script we can use import
follow by the name of the file , without the .py extension.
to make it easier to use we can create aliases for the different script imported
now, in many occasions the is code in this scripts that is not useful, or not useful in a script that is calling them, so in this case we use the "main" block
Using a main block¶
To avoid running executable statements in a script when it's imported as a module in another script, include these lines in an if __name__ == "__main__"
block. Or alternatively, include them in a function called main() and call this in the if main
block.
The code inside the block main will be execute just when we are running the script in specific and not when we are colling it as module in a script.
Most common use Python Standard Library modules¶
The Python Standard Library has a lot of modules, here are a selection of our Python Standard Library modules and why we use them!
csv
: very convenient for reading and writing csv filescollections
: useful extensions of the usual data types including OrderedDict, defaultdict and namedtuplerandom
: generates pseudo-random numbers, shuffles sequences randomly and chooses random itemsstring
: more functions on strings. This module also contains useful collections of letters like string.digits (a string containing all characters which are valid digits).re
: pattern-matching in strings via regular expressionsmath
: some standard mathematical functionsos
: interacting with operating systemsos.path
: submodule of os for manipulating path namessys
: work directly with the Python interpreterjson
: good for reading and writing json files (good for web work)
Techniques for Importing Modules¶
there are some variants of import
that are useful in different situation.
- To import an individual function or class:
- To import multiple individual object from a module:
- To rename a module:
- To import an object from a module and rename it:
Modules, packages, and Names¶
In order to manage the coder better, the modules of python are contain in package, this package is just a container for the modules, to call the sub-module from one package we use the dot notation:
Third-party packages¶
normally in a project you will find a document call requirements.txt which is a list that contain all the modules, packages and its version us in the specific script, the requirements.txt file looks like:
to install all the packages on this file we can use pip, the command will be:- IPython - A better interactive Python interpreter
- requests - Provides easy to use methods to make web requests. Useful for accessing web APIs.
- Flask - a lightweight framework for making web applications and APIs.
- Django - A more featureful framework for making web applications. Django is particularly good for designing complex, content heavy, web applications.
- Beautiful Soup - Used to parse HTML and extract information from it. Great for web scraping.
- pytest - extends Python's builtin assertions and unittest module.
- PyYAML - For reading and writing YAML files.
- NumPy - The fundamental package for scientific computing with Python. It contains among other things a powerful N-dimensional array object and useful linear algebra capabilities.
- pandas - A library containing high-performance, data structures and data analysis tools. In particular, pandas provides dataframes!
- matplotlib - a 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments.
- ggplot - Another 2D plotting library, based on R's ggplot2 library.
- Pillow - The Python Imaging Library adds image processing capabilities to your Python interpreter.
- pyglet - A cross-platform application framework intended for game development.
- Pygame - A set of Python modules designed for writing games.
- pytz - World Timezone Definitions for Python