cloudify-incubator / cloudify-python-importer

Bypass issues with import namespaced modules from sys.path folders(Deprecated)
Apache License 2.0
0 stars 0 forks source link

site dirs may be a better solution #1

Open craynic opened 6 years ago

craynic commented 6 years ago

I found it very ugly to do this tricky work. What this package does is to check the init.py when importing EVERY package, If the file does not exist (means it may be a namespaced package), it creates the file. Many problems come with it.

  1. The package dir may not be writable, which will cause an IOError, and further an ImportError. This makes it failed to import the package, and very hard to debug.
  2. It will search the dirs that is not a package. For example, usr/lib/python2.7/config.
  3. It produced error when importing pycrypto in my environment. I don't know why.

I suggest to use sitedir to import namespaced packages. When calling plugin code, plugin site dirs will be inserted into PYTHONPATH in dispatch module, we can just add the two dirs to sitedir in plugin __init__.py. Python will look for the .pth files in site dirs and make it possible to import namespaced packages. Example:

import sys import site site.addsitedir(sys.path[0]) site.addsitedir(sys.path[1])

0lvin commented 6 years ago

Thank you, i will check.

craynic commented 6 years ago

so how is the check?

0lvin commented 6 years ago

Sorry, i am busy for now with other tasks.

0lvin commented 4 years ago

Code is moved to other repository.

Addsitedir does not help in current case. https://github.com/cloudify-incubator/cloudify-utilities-plugins-sdk/blob/8245d577902d25cae9c246dd3bdd46675de4c675/cloudify_common_sdk/importer.py#L149-L172

We are looking for better solution.