user3734568
user3734568

Reputation: 1481

Creating .pyd files in folder and subfolder using python

I am trying to create .pyd files using below script for all .py files in folder and sub folders of project.

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))

But when I execute above script, it is creating .pyd files by creating one more folder with name "Project". Can anyone help me to know what is wrong in the above script.

Also it will be helpful if there is any alternate option to create .pyd file for all .py files under folders and sub folders

Upvotes: 0

Views: 1058

Answers (1)

aditya verma
aditya verma

Reputation: 1

I have Updated your code with few more lines, try this if it helps:

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)

Upvotes: 0

Related Questions