【问题标题】:Creating .pyd files in folder and subfolder using python使用 python 在文件夹和子文件夹中创建 .pyd 文件
【发布时间】:2021-07-02 04:03:17
【问题描述】:

我正在尝试使用以下脚本为项目文件夹和子文件夹中的所有 .py 文件创建 .pyd 文件。

import os
from distutils.core import setup
from Cython.Build import cythonize
import warnings
warnings.filterwarnings('ignore')

os.chdir(r"C:\Users\Downloads\Project")
             
for parent, dirnames, filenames in os.walk(os.getcwd()):
    for fn in filenames:
        if fn.lower().endswith('.py') and fn !='__init__.py':
            setup(ext_modules=cythonize(parent+ "\\" +fn),script_args=['build'],options={'build':{'build_lib':'.'}})
            os.remove(os.path.join(parent, fn))

但是当我执行上面的脚本时,它会通过创建一个名为“Project”的文件夹来创建 .pyd 文件。 谁能帮我知道上面的脚本有什么问题。

如果有任何替代选项可以为文件夹和子文件夹下的所有 .py 文件创建 .pyd 文件,这也会很有帮助

【问题讨论】:

    标签: python cythonize pyd


    【解决方案1】:

    我已经用几行更新了你的代码,如果有帮助,试试这个:

    import os
    from distutils.core import setup
    from Cython.Build import cythonize
    import warnings
        
    warnings.filterwarnings('ignore')
        
    package_path = r"C:\Users\Downloads\Project"
    module_name = os.path.basename(os.path.normpath(package_path))
    os.chdir(package_path)
        
    for parent, dirnames, filenames in os.walk(os.getcwd()):
        for fn in filenames:
            if fn.lower().endswith('.py') and fn != '__init__.py':
                os.chdir(parent)
                setup(ext_modules=cythonize(parent + "/" + fn), script_args=['build'],
                      options={'build': {'build_lib': '.'}})
                os.remove(os.path.join(parent, fn))
        
    import shutil
        
    source_dir = os.path.join(package_path, module_name)
    file_names = os.listdir(source_dir)
     
    for file_name in file_names:
        shutil.move(os.path.join(source_dir, file_name), package_path)
        
    os.rmdir(source_dir)
    

    【讨论】:

      猜你喜欢
      • 2015-06-07
      • 2018-12-02
      • 2020-07-18
      • 2012-06-03
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多