Show Menu
Cheatography

Python_chap_14 Cheat Sheet (DRAFT) by

Create and Import modules in Python

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Create a module

NA = 6.02e-23 # module-level constant

class Class_1():
    """ 
    #docstrings, output of help(module)
    # id PEP257 chapitre 15

    """
 
    def __init__(self, function_1, function_2, data):
         self.function_1 = function_1
         self.function_2 = function_2
         self.data = xxx
 
     def function_1(self):
         blabla
         return var
 
    def function_2(self):
         print(data)

    def updata(self, add_var): 
    #Modifying an Attribute’s Value Through a Method
        self.data += add_var

class Class_2(Class_1): # inherit from Animal
 """ 
comment for the class2

blabla
"""
    PI = 3.14 # constant with uppercase letters
    # invariable during programm excutation)

    def __init__(self, make, model, year):
         super(class1, self).__init__(function1, function2, data)
         # call the superclass constructor
         blabla

    def function_3(self,):
     """docstrings under method"""
        blabla

    def function_4(self):
        blablabla
The __init__() method takes in these parameters and stores them in the attributes that will be associated with instances made from this class.
 

import a module

import module as alias
# import a entier module and give it an allas 

from module import class
# from a module import a class
# it should prepare at first a file __init__.py at the directory

from module.class import method
# from a class of module import a method (function)

init.py

An init.py file is a special file that is used to indicate that a directory contains a Python package. A Python package is a collection of modules that can be imported and used together. An init.py file can also contain code that is executed when the package or a module in the package is imported. This can be useful for initia­lizing packag­e-level data or performing other tasks

Before Python 3.3, an init.py file was required for every package directory. Otherwise, Python would not recognize the directory as a package and would not be able to import modules from it. This was done to prevent direct­ories with common names, such as string, from uninte­nti­onally hiding valid modules that occur later on the module search path.

However, since Python 3.3, an init.py file is no longer mandatory for defining a package. Python can also recognize namespace packages, which are packages that do not have an init.py file and can span multiple direct­ories. Namespace packages allow multiple portions of a package to be distri­buted and installed separa­tely, and then merged together at runtime

create an enviro­nment path

Linux & MacOS un file bash (~.bashrc)
export PYTHONPATH=$PYTHONPATH:/chemin/vers/mon/super/module
then 
echo $PYTHONPATH
source ~.bashrc

Windows in shell PowerShell
$env:PYTHONPATH += ";C:\chemin\vers\mon\super\module"
then 
echo $env:PYTHONPATH