Aspose.Email python module overwriting base python 'email' module

Hi support,

We are using Aspose.Email-for-Python-via-NET within python. When importing this package alongside the ‘email’ python base module, apose.email overwrites it, preventing the email module from being used. In particular, this prevents access to email.policy provided by python. Is there a way to resolve this issue?

Thank you

Hello @Sijo.Kolambran ,

Thank you for reporting your issue.

It seems like you are encountering a namespace collision issue between the email module in Python’s library and the Aspose.Email module.
When you import Aspose.Email, it might be overshadowing the email module, causing conflicts.

To solve this issue, you can access the email module functionality through the python_email alias, avoiding conflicts with the Aspose.Email module.

An example:

import email as python_email
from Aspose.Email import ...

policy = python_email.policy

Hi,

Unfortunately we are unable to implement this workaround as the email module is being imported from a 3rd party package and so it’s not feasible to edit. Are there other ways to prevent this issue?

Thanks again for your help

Hello @Sijo.Kolambran,

You might consider using the importlib module to dynamically import the Aspose.Email module and manipulate its behavior without directly importing it into your code. This allows you to control the namespace and avoid conflicts with the email module.

import importlib

# Dynamically import the Aspose.Email module with a custom name
aspose_email = importlib.import_module('aspose.email')

# Now you can use 'aspose_email' instead of 'email'
eml = aspose_email.MailMessage.load(file_name);

Hi,

Please let me know if I’m misunderstanding, but this approach doesn’t seem to avoid the base email module being overwritten:

import importlib

aspose_email = importlib.import_module('aspose.email')
print(aspose_email)

import email
print(email)

Output (The second import points to aspose.email):

<module 'email' (C:\...\lib\site-packages\aspose\email.cp39-win_amd64.pyd)>
<module 'email' (C:\...\lib\site-packages\aspose\email.cp39-win_amd64.pyd)>

So similarly, external modules will be affected by the overwrite.

@Sijo.Kolambran ,

Thank you for the provided information. Your issue is under investigation. We’ll let you know the results asap.

@margarita.samodurova and @Sijo.Kolambran , I’m running into this exact problem using Aspose.Email-for-Python-via-NET==24.5. I looked through the release notes for the last couple years and didn’t see a bug fix highlighted. Was this fixed? If so, in what version?

Hello @cjmobley ,

I will clarify the information on your question and provide a reply as soon as possible.

@cjmobley,

Sorry for the confusion. I noticed you’re using aspose-cells-python, so I suggest reaching out on the Aspose.Cells Support Forum. Our colleagues there will be able to assist you with this issue.

@margarita.samodurova , I meant Aspose.Email-for-Python-via-NET==24.5, and I’m getting the same error as above. email.policy is being imported by psycopg which is being imported by sqlalchemy. I tried importing email.policy prior to importing from aspose.email import .... However, this didn’t fix the issue, nor did a shim file. So, it appears that aspose.email is not just colliding with the email namespace. It is actively and aggressively overwriting the sys.modules[‘email’] entry whenever it gets imported. Was this behavior changed in a later release? If not, how would you suggest I fix this issue.

@cjmobley,

This issue should no longer occur in the latest versions of Aspose.Email for Python via .NET.
Please update to the most recent release and confirm whether the problem has been resolved on your side.

Thank you.

I upgraded to aspose-email-for-python-via-net==25.4 and the issue appears to have been resolved. Thank you!

Thanks for letting us know, glad it’s fixed!