【Python】VSCode 中导入Python 自定义不同目录同名包问题

 

目录

1. 问题

2. 需求

3. 解决

4. 遗留问题

5. 参考


 

1. 问题

最近在写Python 代码的过程中,按照Java 大法的习惯,不同的业务写到不同的软件包目录中,然后在IDE 中设置要加载的目录路径,然后在待导入的软件包中引入即可;但是到了Python 中,这种写法我嚓嘞,不行;

找了找资料,然后简单写写;

Python 中是怎么解决,多路径下同包名引用的问题呢 ?

 

2. 需求

实际需求目录结构如下:

+ xxx
| 
| --- + src                   // src 目录, 用来存放源码目录
|     | --- setup.py          // namespace_package=['com_dvsnier_xxx'] setup 声明文件
|     | --- + com             // com package - Python 包
|           | --- __init__.py     // __path__ = ...         
|           | --- + dvsnier
|                 | --- ...
|
|
...
|
| --- + mock                  // mock 目录,用来存放mock 配置数据目录
|     | --- setup.py          // namespace_package=['com_dvsnier_xxx_mock'] setup 声明文件
|     | --- + com             // com package - Python 包
|           | --- __init__.py     // __path__ = ...
|           | --- + dvsnier
|                 | --- ...
|
...
|
|
| --- xxx.py

举例如下:

现在我想要在目录 xxx/xxx.py 文件中分别同时引入如下文件:

# 1. 我想要引用如下路径的 'xxx.py' 文件中的一个函数
# namespace_package=['com_dvsnier_xxx']
xxx/src/com/dvsnier/asc/xxx.py


# 2. 我想要引用如下路径的 'yyy.py' 文件中的一个函数
# namespace_package=['com_dvsnier_xxx_mock']
xxx/mock/com/dvsnier/asc/yyy.py

如果我运行如下命令:

$ cd xxx
$ python xxx.py

此时,直接会暴如下错误

ImportError: No module named asc/yyy.py

为啥是这样的呢 ?

原因是Python 编译器在指定目录环境下,查找package 找到一个同名包后,就不会再找下一个同名包; 导致当前找到的同名包中不含有后续同名包中的子包和模块信息;

 

为啥Python 就不能学Java 那种引用方式合并包名引用编译执行呢?

 

3. 解决

 

如下几项配置信息都是固定项配置项,只要比葫芦画瓢照着改即可;

 

1. 配置xxx/src/setup.py 信息

# -*- coding:utf-8 -*-

from setuptools import setup, find_packages

setup(
    name='dvsnier_xxx',
    version='0.0.1',
    description='the dvsnier xxx package',
    long_description='the dvsnier xxx package',
    author='dvsnier',
    author_email='[email protected]',
    license='Apache Software License',
    packages=find_packages(),
    platforms="any",
    zip_safe=False,
)

 

2. 配置xxx/src/com/__init__.py 信息

# -*- coding:utf-8 -*-

__path__ = __import__('pkgutil').extend_path(__path__, __name__)

 

3. 配置xxx/mock/setup.py 信息

# -*- coding:utf-8 -*-

from setuptools import setup, find_packages

setup(
    name='dvsnier_xxx_mock',
    version='0.0.1',
    description='the dvsnier xxx mock package',
    long_description='the dvsnier xxx mock package',
    author='dvsnier',
    author_email='[email protected]',
    license='Apache Software License',
    packages=find_packages(),
    platforms="any",
    zip_safe=False,
)

 

4. 配置xxx/mock/com/__init__.py 信息

# -*- coding:utf-8 -*-

__path__ = __import__('pkgutil').extend_path(__path__, __name__)

 

5. 配置好如上几项内容后,在当前项目目录(此处举例项目目录为:xxx):

执行dvsnier_xxx 包引导安装操作,

$ cd src
$ pip2 install . --log ~/dvsnier_xxx_install.log

执行过程,如下所示:

2019-12-30T19:35:07,501 DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
2019-12-30T19:35:07,517 Created temporary directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-ephem-wheel-cache-uqS1Jn
2019-12-30T19:35:07,518 Created temporary directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-tracker-xiQGZu
2019-12-30T19:35:07,518 Created requirements tracker '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-tracker-xiQGZu'
2019-12-30T19:35:07,518 Created temporary directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-install-tXVtpG
2019-12-30T19:35:07,519 Processing /Users/dvsnier/Documents/xxx/src
2019-12-30T19:35:07,519   Created temporary directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4
2019-12-30T19:35:07,553   Added file:///Users/dvsnier/Documents/xxx/src to build tracker '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-tracker-xiQGZu'
2019-12-30T19:35:07,558     Running setup.py (path:/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4/setup.py) egg_info for package from file:///Users/dvsnier/Documents/xxx/src
2019-12-30T19:35:07,559     Running command python setup.py egg_info
2019-12-30T19:35:07,826     running egg_info
2019-12-30T19:35:07,827     creating /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4/pip-egg-info/dvsnier_xxx.egg-info
2019-12-30T19:35:07,827     writing /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4/pip-egg-info/dvsnier_xxx.egg-info/PKG-INFO
2019-12-30T19:35:07,828     writing top-level names to /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4/pip-egg-info/dvsnier_xxx.egg-info/top_level.txt
2019-12-30T19:35:07,828     writing dependency_links to /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4/pip-egg-info/dvsnier_xxx.egg-info/dependency_links.txt
2019-12-30T19:35:07,828     writing manifest file '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4/pip-egg-info/dvsnier_xxx.egg-info/SOURCES.txt'
2019-12-30T19:35:07,836     reading manifest file '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4/pip-egg-info/dvsnier_xxx.egg-info/SOURCES.txt'
2019-12-30T19:35:07,837     writing manifest file '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4/pip-egg-info/dvsnier_xxx.egg-info/SOURCES.txt'
2019-12-30T19:35:07,852   Source in /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4 has version 0.0.1, which satisfies requirement dvsnier-xxx==0.0.1 from file:///Users/dvsnier/Documents/xxx/src
2019-12-30T19:35:07,852   Removed dvsnier-xxx==0.0.1 from file:///Users/dvsnier/Documents/xxx/src from build tracker '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-tracker-xiQGZu'
2019-12-30T19:35:07,855 Building wheels for collected packages: dvsnier-xxx
2019-12-30T19:35:07,856   Created temporary directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-wheel-bSf4I0
2019-12-30T19:35:07,856   Destination directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-wheel-bSf4I0
2019-12-30T19:35:07,856   Running command /Users/dvsnier/Documents/xxx/venv2/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4/setup.py'"'"'; __file__='"'"'/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-wheel-bSf4I0 --python-tag cp27
2019-12-30T19:35:08,069   running bdist_wheel
2019-12-30T19:35:08,077   running build
2019-12-30T19:35:08,077   running build_py
2019-12-30T19:35:08,078   creating build
2019-12-30T19:35:08,079   creating build/lib
2019-12-30T19:35:08,079   creating build/lib/com
2019-12-30T19:35:08,079   copying com/__init__.py -> build/lib/com
2019-12-30T19:35:08,080   creating build/lib/com/dvsnier
2019-12-30T19:35:08,080   copying com/dvsnier/__init__.py -> build/lib/com/dvsnier
2019-12-30T19:35:08,090   ...
2019-12-30T19:35:08,090   installing to build/bdist.macosx-10.15-x86_64/wheel
2019-12-30T19:35:08,091   running install
2019-12-30T19:35:08,091   running install_lib
2019-12-30T19:35:08,094   creating build/bdist.macosx-10.15-x86_64
2019-12-30T19:35:08,094   creating build/bdist.macosx-10.15-x86_64/wheel
2019-12-30T19:35:08,095   creating build/bdist.macosx-10.15-x86_64/wheel/com
2019-12-30T19:35:08,095   copying build/lib/com/__init__.py -> build/bdist.macosx-10.15-x86_64/wheel/com
2019-12-30T19:35:08,095   creating build/bdist.macosx-10.15-x86_64/wheel/com/dvsnier
2019-12-30T19:35:08,095   creating build/bdist.macosx-10.15-x86_64/wheel/com/dvsnier/tools
2019-12-30T19:35:08,100   ...
2019-12-30T19:35:08,104   running install_egg_info
2019-12-30T19:35:08,111   running egg_info
2019-12-30T19:35:08,111   creating dvsnier_xxx.egg-info
2019-12-30T19:35:08,112   writing dvsnier_xxx.egg-info/PKG-INFO
2019-12-30T19:35:08,112   writing top-level names to dvsnier_xxx.egg-info/top_level.txt
2019-12-30T19:35:08,112   writing dependency_links to dvsnier_xxx.egg-info/dependency_links.txt
2019-12-30T19:35:08,112   writing manifest file 'dvsnier_xxx.egg-info/SOURCES.txt'
2019-12-30T19:35:08,114   reading manifest file 'dvsnier_xxx.egg-info/SOURCES.txt'
2019-12-30T19:35:08,115   writing manifest file 'dvsnier_xxx.egg-info/SOURCES.txt'
2019-12-30T19:35:08,115   Copying dvsnier_xxx.egg-info to build/bdist.macosx-10.15-x86_64/wheel/dvsnier_xxx-0.0.1-py2.7.egg-info
2019-12-30T19:35:08,116   running install_scripts
2019-12-30T19:35:08,139   creating build/bdist.macosx-10.15-x86_64/wheel/dvsnier_xxx-0.0.1.dist-info/WHEEL
2019-12-30T19:35:08,139   creating '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-wheel-bSf4I0/dvsnier_xxx-0.0.1-cp27-none-any.whl' and adding 'build/bdist.macosx-10.15-x86_64/wheel' to it
2019-12-30T19:35:08,140   adding 'com/__init__.py'
2019-12-30T19:35:08,140   adding 'com/dvsnier/__init__.py'
2019-12-30T19:35:08,140   ...
2019-12-30T19:35:08,146   adding 'dvsnier_xxx-0.0.1.dist-info/METADATA'
2019-12-30T19:35:08,146   adding 'dvsnier_xxx-0.0.1.dist-info/WHEEL'
2019-12-30T19:35:08,146   adding 'dvsnier_xxx-0.0.1.dist-info/top_level.txt'
2019-12-30T19:35:08,146   adding 'dvsnier_xxx-0.0.1.dist-info/RECORD'
2019-12-30T19:35:08,147   removing build/bdist.macosx-10.15-x86_64/wheel
2019-12-30T19:35:08,160   Created wheel for dvsnier-xxx: filename=dvsnier_xxx-0.0.1-cp27-none-any.whl size=21935 sha256=0ce28e6162af3a39cbd39c716abaf082ffa50ebd00796203dafae02d78180e20
2019-12-30T19:35:08,160   Stored in directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-ephem-wheel-cache-uqS1Jn/wheels/3c/0d/aa/7dc284cc4dc04cee649c977c1c93d929c7bd8e7b85bc8a557b
2019-12-30T19:35:08,161   Removing source in /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-vLIPi4
2019-12-30T19:35:08,180 Successfully built dvsnier-xxx
2019-12-30T19:35:08,246 Installing collected packages: dvsnier-xxx
2019-12-30T19:35:08,247   Found existing installation: dvsnier-xxx 0.0.1
2019-12-30T19:35:08,261     Uninstalling dvsnier-xxx-0.0.1:
2019-12-30T19:35:08,262       Created temporary directory: /Users/dvsnier/Documents/xxx/venv2/lib/python2.7/site-packages/~om
2019-12-30T19:35:08,263       Removing file or directory /Users/dvsnier/Documents/xxx/venv2/lib/python2.7/site-packages/com/
2019-12-30T19:35:08,263       Created temporary directory: /Users/dvsnier/Documents/xxx/venv2/lib/python2.7/site-packages/~vsnier_gitstat-0.0.1.dist-info
2019-12-30T19:35:08,263       Removing file or directory /Users/dvsnier/Documents/xxx/venv2/lib/python2.7/site-packages/dvsnier_xxx-0.0.1.dist-info/
2019-12-30T19:35:08,263       Successfully uninstalled dvsnier-xxx-0.0.1

2019-12-30T19:35:08,320 Successfully installed dvsnier-xxx-0.0.1
2019-12-30T19:35:08,320 Cleaning up...
2019-12-30T19:35:08,326 Removed build tracker '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-tracker-xiQGZu'

其实去~/dvsnier_xxx_install.log 目录下看这个日志文件即可看到如上的执行过程,成功的话如上记录,失败的话,会有失败的记录;

由于我之前已经安装过该版本了,所以重复使用如上命令安装,同版本的包会先卸载后再安装;

 

执行dvsnier_xxx_mock 包引导安装操作,

$ cd ..
$ cd mock
$ pip2 install . --log ~/dvsnier_xxx_mock_install.log

执行过程,如下所示:

2019-12-30T19:40:59,282 DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
2019-12-30T19:40:59,297 Created temporary directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-ephem-wheel-cache-NjASd1
2019-12-30T19:40:59,297 Created temporary directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-tracker-1_thQv
2019-12-30T19:40:59,297 Created requirements tracker '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-tracker-1_thQv'
2019-12-30T19:40:59,298 Created temporary directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-install-xToWKp
2019-12-30T19:40:59,299 Processing /Users/dvsnier/Documents/xxx/mock
2019-12-30T19:40:59,299   Created temporary directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO
2019-12-30T19:40:59,309   Added file:///Users/dvsnier/Documents/xxx/mock to build tracker '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-tracker-1_thQv'
2019-12-30T19:40:59,309     Running setup.py (path:/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO/setup.py) egg_info for package from file:///Users/dvsnier/Documents/xxx/mock
2019-12-30T19:40:59,309     Running command python setup.py egg_info
2019-12-30T19:40:59,587     running egg_info
2019-12-30T19:40:59,588     creating /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO/pip-egg-info/dvsnier_xxx_mock.egg-info
2019-12-30T19:40:59,588     writing /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO/pip-egg-info/dvsnier_xxx_mock.egg-info/PKG-INFO
2019-12-30T19:40:59,589     writing top-level names to /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO/pip-egg-info/dvsnier_xxx_mock.egg-info/top_level.txt
2019-12-30T19:40:59,589     writing dependency_links to /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO/pip-egg-info/dvsnier_xxx_mock.egg-info/dependency_links.txt
2019-12-30T19:40:59,589     writing manifest file '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO/pip-egg-info/dvsnier_xxx_mock.egg-info/SOURCES.txt'
2019-12-30T19:40:59,598     reading manifest file '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO/pip-egg-info/dvsnier_xxx_mock.egg-info/SOURCES.txt'
2019-12-30T19:40:59,599     writing manifest file '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO/pip-egg-info/dvsnier_xxx_mock.egg-info/SOURCES.txt'
2019-12-30T19:40:59,610   Source in /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO has version 0.0.1, which satisfies requirement dvsnier-xxx-mock==0.0.1 from file:///Users/dvsnier/Documents/xxx/mock
2019-12-30T19:40:59,611   Removed dvsnier-xxx-mock==0.0.1 from file:///Users/dvsnier/Documents/xxx/mock from build tracker '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-tracker-1_thQv'
2019-12-30T19:40:59,624 Building wheels for collected packages: dvsnier-xxx-mock
2019-12-30T19:40:59,625   Created temporary directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-wheel-rpNXXZ
2019-12-30T19:40:59,625   Destination directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-wheel-rpNXXZ
2019-12-30T19:40:59,625   Running command /Users/dvsnier/Documents/xxx/venv2/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO/setup.py'"'"'; __file__='"'"'/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-wheel-rpNXXZ --python-tag cp27
2019-12-30T19:40:59,807   running bdist_wheel
2019-12-30T19:40:59,816   running build
2019-12-30T19:40:59,816   running build_py
2019-12-30T19:40:59,817   creating build
2019-12-30T19:40:59,820   creating build/lib
2019-12-30T19:40:59,820   creating build/lib/com
2019-12-30T19:40:59,820   copying com/__init__.py -> build/lib/com
2019-12-30T19:40:59,820   creating build/lib/com/dvsnier
2019-12-30T19:40:59,821   copying com/dvsnier/__init__.py -> build/lib/com/dvsnier
2019-12-30T19:40:59,821   creating build/lib/com/dvsnier/mock
2019-12-30T19:40:59,823   installing to build/bdist.macosx-10.15-x86_64/wheel
2019-12-30T19:40:59,823   running install
2019-12-30T19:40:59,823   running install_lib
2019-12-30T19:40:59,826   creating build/bdist.macosx-10.15-x86_64
2019-12-30T19:40:59,826   creating build/bdist.macosx-10.15-x86_64/wheel
2019-12-30T19:40:59,826   creating build/bdist.macosx-10.15-x86_64/wheel/com
2019-12-30T19:40:59,826   copying build/lib/com/__init__.py -> build/bdist.macosx-10.15-x86_64/wheel/com
2019-12-30T19:40:59,827   creating build/bdist.macosx-10.15-x86_64/wheel/com/dvsnier
2019-12-30T19:40:59,827   creating build/bdist.macosx-10.15-x86_64/wheel/com/dvsnier/mock
2019-12-30T19:40:59,828   copying build/lib/com/dvsnier/__init__.py -> build/bdist.macosx-10.15-x86_64/wheel/com/dvsnier
2019-12-30T19:40:59,828   running install_egg_info
2019-12-30T19:40:59,835   running egg_info
2019-12-30T19:40:59,835   creating dvsnier_xxx_mock.egg-info
2019-12-30T19:40:59,835   writing dvsnier_xxx_mock.egg-info/PKG-INFO
2019-12-30T19:40:59,835   writing top-level names to dvsnier_xxx_mock.egg-info/top_level.txt
2019-12-30T19:40:59,835   writing dependency_links to dvsnier_xxx_mock.egg-info/dependency_links.txt
2019-12-30T19:40:59,836   writing manifest file 'dvsnier_xxx_mock.egg-info/SOURCES.txt'
2019-12-30T19:40:59,837   reading manifest file 'dvsnier_xxx_mock.egg-info/SOURCES.txt'
2019-12-30T19:40:59,837   writing manifest file 'dvsnier_xxx_mock.egg-info/SOURCES.txt'
2019-12-30T19:40:59,837   Copying dvsnier_xxx_mock.egg-info to build/bdist.macosx-10.15-x86_64/wheel/dvsnier_xxx_mock-0.0.1-py2.7.egg-info
2019-12-30T19:40:59,839   running install_scripts
2019-12-30T19:40:59,861   creating build/bdist.macosx-10.15-x86_64/wheel/dvsnier_xxx_mock-0.0.1.dist-info/WHEEL
2019-12-30T19:40:59,861   creating '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-wheel-rpNXXZ/dvsnier_xxx_mock-0.0.1-cp27-none-any.whl' and adding 'build/bdist.macosx-10.15-x86_64/wheel' to it
2019-12-30T19:40:59,862   adding 'com/__init__.py'
2019-12-30T19:40:59,862   adding 'com/dvsnier/__init__.py'
2019-12-30T19:40:59,862   ...
2019-12-30T19:40:59,863   removing build/bdist.macosx-10.15-x86_64/wheel
2019-12-30T19:40:59,877   Created wheel for dvsnier-xxx-mock: filename=dvsnier_xxx_mock-0.0.1-cp27-none-any.whl size=2115 sha256=0bf16d02f3a6ee32c6b7591863fe8acc2e587546a07a6db6f6806c9bbd71906a
2019-12-30T19:40:59,877   Stored in directory: /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-ephem-wheel-cache-NjASd1/wheels/3d/49/75/546c769a1ba98492f5ba5278792f839bf2df39581bee73a4eb
2019-12-30T19:40:59,878   Removing source in /private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-build-fbm_tO
2019-12-30T19:40:59,887 Successfully built dvsnier-xxx-mock
2019-12-30T19:40:59,948 Installing collected packages: dvsnier-xxx-mock

2019-12-30T19:40:59,970 Successfully installed dvsnier-xxx-mock-0.0.1
2019-12-30T19:40:59,970 Cleaning up...
2019-12-30T19:40:59,973 Removed build tracker '/private/var/folders/qx/1kbn17z96bb_7ly6sw7bgv2m0000gn/T/pip-req-tracker-1_thQv'

其实去~/dvsnier_xxx_mock_install.log 目录下看这个日志文件即可看到如上的执行过程,成功的话如上记录,失败的话,会有失败的记录;

由于我之前已经安装过该版本了,所以重复使用如上命令安装,同版本的包会先卸载后再安装;

 

此时,在你的python2 虚拟环境目录下,即可看到刚安装的引用包,如下图所示:

【Python】VSCode 中导入Python 自定义不同目录同名包问题_第1张图片

 

 

4. 遗留问题

按照如上操作步骤执行的话,此时,在xxx.py 文件中加入如下引用:

# -*- coding:utf-8 -*-

import sys
import os

sys.path.append(os.path.join(os.getcwd(), "src"))
sys.path.append(os.path.join(os.getcwd(), "mock"))


from com.dvsnier.asc import xxx
from com.dvsnier.asc import yyy


# then process custom logical
#
# ...

if __name__ == '__main__':
    pass

 

这样是可以引用的,但是做成类似于架包的形式,就无法直接引用xxx/src, xxx/mock 里面的内容,做到修改即所得的效果;

既然已经安装到本地虚拟环境site-packages 包目录中当去了,上面的:

import sys
import os

sys.path.append(os.path.join(os.getcwd(), "src"))
sys.path.append(os.path.join(os.getcwd(), "mock"))

这几句也是可以删除掉的,即删除掉照样可以引用到相关包中的文件;

 

为啥这样写呢 ?保留的作用是啥呢 ?

1. 考虑到大多数场景下,其实我们可以写在同一个目录下,然后屏蔽多目录同包名的问题;

2. 还有一种解决方式就是,在外层再套一层包,让同名的变为各个外层互异包的子包,也能屏蔽多目录同包名带来的问题;

3. 可以直接源码引用, 所改即所得;

 

目前这块自己还有待继续研究一下,后续有时间了再更新吧;

...

 

5. 参考

  1. Python打包用户指南;
  2. packaging.python.org-sample;
  3. Packaging namespace packages;
  4. Packaging namespace packages
  5. 模块;
  6. setuptools 42.0.2 文档;
  7. sample-namespace-packages;
  8. How do I create a namespace package in Python?
  9. No module named pkg_resources;

 

你可能感兴趣的:(Python,Python)