Addressed an issue where IIS couldn't import a pyd file in a Django project

TL;DR https://qrunch.net/@sugurunatsuno/entries/wF9aXWZ3x3PBarLR Add the directory containing the pyd file you want to read to PYTHONPATH.

Introduction

I can't load my pyd file when I run a Django project using IIS. As a result, the environment variable PYTHONPATH did not have a directory that could refer to the pyd file. I was able to import the pyd file when I ran it on Django's runserver. It is assumed that IIS will work except for importing pyd files.

version

OS/software/Library name version
Windows 10
IIS 10
Python 3.8.0
Django 2.2.8
Cython 0.29.15

When setting with IIS

It only needs to be set in either IIS or Python. In IIS, environment variables can be set with web.config etc. You can set multiple paths separated by semicolons, so add the path where the pyd file is located.

<add key="PYTHONPATH" value="project_path;pyd_absolute_path" />

Since the import of the pyd file in this case refers to PYTHONPATH, it is not a relative path from the py file to be called.

#Cannot be referenced by relative path
# from pyd_relative_path import target

#Since it is set in PYTHONPATH, it can be called as it is
import target

When setting on Python

It only needs to be set in either IIS or Python. Environment variables can also be set on python, so you can set the directory where the pyd file is located.

import os
import sys

current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path += [os.path.join(current_dir, 'pyd_relative_path')]

import target

Recommended Posts

Addressed an issue where IIS couldn't import a pyd file in a Django project
Create an executable file in a scripting language
Run a Python file with relative import in PyCharm
How to reference static files in a Django project
Start a Django project
[Sublime Text 2] Always execute a specific file in the project
How to import a file anywhere you like in Python
[Note] Import of a file in the parent directory in Python