alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[1028] Creating your very own Python library (package)

Ah, creating your very own Python library—how exciting! 🐍 Let’s dive into the process step by step so you can have your custom goodies ready for import.

  1. Creating Your Python Library:

    • First, you’ll want to write your Python code and organize it into a package or module. A package can contain multiple modules, and a module is simply a .py file with functions, classes, or variables.
    • For example, let’s say you’re creating a library called “myawesomeutils.” You’d structure it like this:
      myawesomeutils/
      ├── __init__.py
      ├── utils.py
      └── other_module.py
    • In utils.py, you’d define your utility functions or classes.
    • Creating the file __init__.py is recommended because the existence of an __init__.py file allows users to import the directory as a regular package, even if (as is the case in this tutorial) __init__.py is empty. utils.py and other_module.py can be used to save different kinds of methods, but how to call all the functions from myawesomeutils? We should add some codes in the __init__.py file.
      from myawesomeutils.utils import * # Add all functions from myawesomeutils.utils
      from myawesomeutils.other_module import * # Add all functions from myawesomeutils.other_module
      # Then all above functions can be used by myawesomeutils
  2. Packaging Your Library:

    • To make your library installable and shareable, you need to package it. Python has a fantastic packaging ecosystem, and it’s easier than you might think.
    • Create a setup.py file in your library’s root directory. This file contains metadata about your package (name, version, author, etc.). Here’s a minimal example:
      # setup.py
      from setuptools import setup, find_packages
      setup(
      name="myawesomeutils",
      version="0.1.0",
      packages=find_packages(),
      )
    • Note that find_packages() automatically discovers all subpackages and modules.
    • The file structure is like this:
      setup.py
      myawesomeutils/
      ├── __init__.py
      ├── utils.py
      └── other_module.py
  3. Building Your Package:

    • Open a terminal and navigate to your library’s root directory.
    • Run:
      python setup.py sdist bdist_wheel
    • This command creates a source distribution (sdist) and a wheel distribution (bdist_wheel). The wheel format is preferred because it’s more efficient.
    • If it raise the error, maybe it's because you haven't install wheel, which can be done like this:
      python -m pip install wheel
    • Folders are like this:
  4. Installing Your Library Locally:

    • After building, you’ll find a dist directory containing your distribution files.
    • Install your library locally using pip:
      python -m pip install dist\myawesomeutils-0.1.0-py3-none-any.whl
  5. Testing Your Library:

    • Create a test script (e.g., test_myawesomeutils.py) where you import and use your library.
    • Run the test script to ensure everything works as expected.
    • An example:
      import myawesomeutils # It can call all the functions from both the util.py and other_module files
      myawesomeutils.func1() # func1() is defined in the util.py file
      # Output: This is utils.py!
      myawesomeutils.func2() # func2() is defined in the other_module.py file
      # Output: This is other_module.py!
  6. Adding to site-packages:

    • Now, let’s make your library globally accessible. You have a couple of options:
      • Option 1: User Site Directory
        • This is a user-specific location where Python looks for additional packages.
        • Find the user site directory:
          python -m site --user-site
        • Create your .pth file (e.g., myawesomeutils.pth) in that directory. The .pth file should contain the path to your library (e.g., /path/to/myawesomeutils).
      • Option 2: System-wide Site Packages
        • If you have admin privileges, you can add your library directly to the system-wide site packages.
        • Locate your system’s site-packages directory (e.g., /usr/local/lib/pythonX.X/site-packages).
        • Copy or symlink your library there.
  7. Using Your Library:

    • Now, wherever you are, you can simply:
      import myawesomeutils

And voilà! Your custom Python library is ready for action. 🎉 Remember, creating libraries is like building little Lego blocks for other developers to use. So go ahead, share your knowledge and creativity with the world! If you have any more questions or need assistance, just holler—I’m here! 😊🚀12

Learn more:

posted on   McDelfino  阅读(6)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-07-11 【857】R语言实现字符串操作、补零操作
2023-07-11 【856】R语言palette查找
2022-07-11 【727】latex相同作者的参考文献被横线代替
2021-07-11 【602】Python代码中的三个点(...)
2021-07-11 【602】语义分割评价指标 IoU mIoU precision recall F1 的计算
2012-07-11 【058】英语词根词缀记忆大全【转】
点击右上角即可分享
微信分享提示