GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialsteacher.com — Python Packages Python - PackagesWe organize a large number of files in different folders and subfolders based on some criteria, so that we can find and manage them easily. In the same way, a package in Python takes the concept of the modular approach to next logical level. As you know, a module can contain multiple objects, such as classes, functions, etc. A package can contain one or more relevant modules. Physically, a package is actually a folder containing one or more module files. Let's create a package named mypackage, using the following steps:
greet.py functions.py That's it. We have created our package called mypackage. The following is a folder structure: ![]() Importing a Module from a PackageNow, to test our package, navigate the command prompt to the Import the functions module from the mypackage package and call its power() function. >>> from mypackage import functions>>> functions.power(3,2) 9 It is also possible to import specific functions from a module in the package. >>> from mypackage.functions import sum>>> sum(10,20) 30 >>> average(10,12) Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> NameError: name 'average' is not defined __init__.pyThe package folder contains a special file called
An empty We shall also create another Python script in the The __init__.py The specified functions can now be imported in the interpreter session or another executable script. Create test.py Note that functions Hello world power(3,2) : 9 Install a Package GloballyOnce a package is created, it can be installed for system-wide use by running the setup script. The script calls Let's install mypackage for system-wide use by running a setup script. Save the following code as setup.py in the parent folder Example: setup.py Now execute the following command to install Processing d:\MyApp Installing collected packages: mypack Running setup.py install for mypack ... done Successfully installed mypackage-0.1 Now mypackage is available for system-wide use and can be imported in any script or interpreter. D:\>python>>> import mypackage >>>mypackage.average(10,20) 15.0 >>>mypackage.power(10,2) 100 You may also want to publish the package for public use. PyPI (stands for Python Package Index) is a repository of Python packages. Visit https://packaging.python.org/distributing to know more about the procedure of uploading a package to PyPI. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |