__import__('pkg_resources').declare_namespace(__name__)

该博客探讨了Python安装包过程中如何使用`pkg_resources.declare_namespace(__name__)`来声明命名空间包。通过解释pkg_resources的作用,_import_的隐式导入功能以及在proj_ex核心代码包中的应用,文章帮助读者理解这一技术。参考了Stack Overflow上的相关讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件:

project/
    setup.py
    proj_ex/
        __init__.py
        corecode.py
>>pip install xxx

安装包过程:
先找到setup.py

from setuptools import setup

setup(
    ...
    name_space_packages = ['proj_ex']
    ...
)

name_space_packages 指向核心代码包:proj_ex

# __init__.py

__import__("pkg_resources").declare_namespace(__name__)

(1) pkg_resources是一个安装包的工具
(2) _import_()加载导入,import的隐式版
(3) 此处的_name_指的是proj_ex
(4) corecode.py加载到proj_ex的命名空间之中

参考:
[1] https://stackoverflow.com/questions/7785944/what-does-import-pkg-resources-declare-namespace-name-do/37104834
[2] https://stackoverflow.com/questions/5064951/packaging-common-python-namespaces

D:\python\program\Lib\site-packages\flask_admin\contrib\__init__.py:2: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81. __import__('pkg_resources').declare_namespace(__name__) Traceback (most recent call last): File "D:\python\program\Lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc return self._engine.get_loc(casted_key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas\\_libs\\hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\\_libs\\hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: '邮编' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "d:\xw\22300434王渝钦结课项目1\app.py", line 97, in <module> initialize_data() File "d:\xw\22300434王渝钦结课项目1\app.py", line 36, in initialize_data import_data() File "d:\xw\22300434王渝钦结课项目1\app.py", line 53, in import_data postal_code=row['邮编'], ~~~^^^^^^^^ File "D:\python\program\Lib\site-packages\pandas\core\series.py", line 1121, in __getitem__ return self._get_value(key) ^^^^^^^^^^^^^^^^^^^^ File "D:\python\program\Lib\site-packages\pandas\core\series.py", line 1237, in _get_value loc = self.index.get_loc(label) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\program\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc raise KeyError(key) from err KeyError: '邮编'
06-26
(rasa) unitree@ubuntu:~/rasa_ws$ rasa run --enable-api /home/unitree/rasa/lib/python3.8/site-packages/rasa/core/tracker_store.py:1044: MovedIn20Warning: Deprecated API features detected! These feature(s) are not compatible with SQLAlchemy 2.0. To prevent incompatible upgrades prior to updating applications, ensure requirements files are pinned to "sqlalchemy<2.0". Set environment variable SQLALCHEMY_WARN_20=1 to show all deprecation warnings. Set environment variable SQLALCHEMY_SILENCE_UBER_WARNING=1 to silence this message. (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9) Base: DeclarativeMeta = declarative_base() /home/unitree/rasa/lib/python3.8/site-packages/rasa/shared/utils/validation.py:134: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html import pkg_resources /home/unitree/rasa/lib/python3.8/site-packages/pkg_resources/__init__.py:3117: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('mpl_toolkits')`. Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages declare_namespace(pkg) /home/unitree/rasa/lib/python3.8/site-packages/pkg_resources/__init__.py:3117: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('ruamel')`. Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages declare_namespace(pkg) /home/unitree/rasa/lib/python3.8/site-packages/sanic_cors/extension.py:39: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead. SANIC_VERSION = LooseVersion(sanic_version) Traceback (most recent call last): File "/home/unitree/rasa/lib/python3.8/site-packages/sklearn/__check_build/__init__.py", line 48, in <module> from ._check_build import check_build # noqa ImportError: /home/unitree/rasa/lib/python3.8/site-packages/sklearn/__check_build/../../scikit_learn.libs/libgomp-d22c30c5.so.1.0.0: cannot allocate memory in static TLS block During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/unitree/rasa/bin/rasa", line 8, in <module> sys.exit(main()) File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/__main__.py", line 133, in main cmdline_arguments.func(cmdline_arguments) File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/cli/run.py", line 93, in run rasa.run(**vars(args)) File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/api.py", line 36, in run import rasa.core.run File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/core/run.py", line 25, in <module> from rasa import server, telemetry File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/server.py", line 59, in <module> from rasa.core.agent import Agent File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/core/agent.py", line 22, in <module> from rasa.core.policies.policy import PolicyPrediction File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/core/policies/policy.py", line 26, in <module> from rasa.core.featurizers.tracker_featurizers import TrackerFeaturizer File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/core/featurizers/tracker_featurizers.py", line 31, in <module> from rasa.core.featurizers.single_state_featurizer import SingleStateFeaturizer File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/core/featurizers/single_state_featurizer.py", line 8, in <module> from rasa.nlu.extractors.extractor import EntityTagSpec File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/nlu/extractors/extractor.py", line 30, in <module> import rasa.utils.train_utils File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/utils/train_utils.py", line 32, in <module> from rasa.utils.tensorflow.data_generator import RasaBatchDataGenerator File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/utils/tensorflow/data_generator.py", line 10, in <module> from rasa.utils.tensorflow.model_data import RasaModelData, Data, FeatureArray File "/home/unitree/rasa/lib/python3.8/site-packages/rasa/utils/tensorflow/model_data.py", line 21, in <module> from sklearn.model_selection import train_test_split File "/home/unitree/rasa/lib/python3.8/site-packages/sklearn/__init__.py", line 81, in <module> from . import __check_build # noqa: F401 File "/home/unitree/rasa/lib/python3.8/site-packages/sklearn/__check_build/__init__.py", line 50, in <module> raise_build_error(e) File "/home/unitree/rasa/lib/python3.8/site-packages/sklearn/__check_build/__init__.py", line 31, in raise_build_error raise ImportError( ImportError: /home/unitree/rasa/lib/python3.8/site-packages/sklearn/__check_build/../../scikit_learn.libs/libgomp-d22c30c5.so.1.0.0: cannot allocate memory in static TLS block ___________________________________________________________________________ Contents of /home/unitree/rasa/lib/python3.8/site-packages/sklearn/__check_build: __pycache__ setup.py __init__.py _check_build.cpython-38-aarch64-linux-gnu.so ___________________________________________________________________________ It seems that scikit-learn has not been built correctly. If you have installed scikit-learn from source, please do not forget to build the package before using it: run `python setup.py install` or `make` in the source directory. If you have used an installer, please check that it is suited for your Python version, your operating system and your platform. 这是我出现的错误
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值