3

I noticed a file include.txt in every project I open. The file contains python code:

from aws_xray_sdk.core import patch_all
import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration

# pragma: no cover
patch_all()

sentry_sdk.init(
    dsn=...,
    integrations=[AwsLambdaIntegration()]
)

That file is then being executed with execfile(os.path...+'/include.txt') in multiple files within that project.

I'm sure the code is necessary. But why not name it include.py and then import it?

I have just recently joined the company, so I have asked a few people who have been around the longest, and nobody knows why.

I'm tempted to rename the txt to py and change execfile to import, but I'm worried that maybe there was a good reason for doing it that way. Possibly my change would appear to look fine but then have some distant unforeseen consequences.

So, is there ever a good reason put python code in a txt file?

UPDATE:

We're using python 3.7.

Nothing from the include.txt is mentioned anywhere else in the project code -- no references to sentry or X-ray.

In every module, execfile is imported just before the call:

from past.builtins import execfile
eternal_student
  • 626
  • 4
  • 18
  • `import` is different because the module has its own namespace. `execfile()` executes the code in the current script's namespace. – Barmar Mar 05 '21 at 16:25
  • 1
    Note that `execfile` is gone in Python 3. See https://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3 – Barmar Mar 05 '21 at 16:26
  • 2
    Maybe you have a compatibility library loaded? The name isn't defined in 3.9.2. – Barmar Mar 05 '21 at 16:40
  • 1
    My bet is someone made https://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3#16577427 into a function that’s also imported in the same module. – Kirk Strauser Mar 05 '21 at 16:46
  • 1
    It's `from past.builtins import execfile` – eternal_student Mar 05 '21 at 17:02

0 Answers0