I wanto dynamically import `module` from app.py an...
# general
a
I wanto dynamically import
module
from app.py and it's getting no module found error
w
regardless of whether you want to import it dynamically or not, you'll need BUILD files that declare the dependencies
which might look like:
Copy code
-src
 - main
   - python
    - company
     - package1
       - BUILD
       - app.py
       - module.py
where
BUILD
contained...
a
I have BUILD file
Copy code
python_binary(
  name = 'cloudwatch_to_tsd',
  entry_point = '<http://pinterest.cloudwatch_to_tsd.app:main|pinterest.cloudwatch_to_tsd.app:main>',
  dependencies = [
    ':_cloudwatch_to_tsd'
  ]
)

python_library(
  name = '_cloudwatch_to_tsd',
  sources = rglobs('*.py'),
  dependencies = [
    '3rdparty/python:boto3',
    '3rdparty/python:brood',
    '3rdparty/python:pyknox',
    '3rdparty/python:schedule',
    '3rdparty/python:pinstatsd',
    '3rdparty/python:metrics-agent'
  ]
)
w
where is it located?
a
it only has dependencies to 3rd party libraray
under cloudwatch_to_tsd which is
package1
in my example
w
the python_library should likely be in a BUILD file at the path i mentioned above, because of "source roots": see https://www.pantsbuild.org/setup_repo.html#source-roots
a
Copy code
-src
 - main
   - python
    - company
     - cloudwatch_to_tsd
       - BUILD
       - app.py
       - module.py
w
the target should be declared somewhere under
src/main/python/**
, which will cause the package names to be relative to that path
ok
so then you should be able to import it using the package name relative to
src/main/python
which looks like
from company.cloudwatch_to_tsd import module
a
importlib.import_module(company.cloudwatch_to_tsd .module)
?
I am trying to use dynamic import
cw_module = importlib.import_module('module')
works with out pex
but if I run with pex, it failed to find module, looks like the path is not in the
PYTHONPATH
w
would unzip the pex and see what you see inside of it.
(a pex is a zip file with a header)
so
unzip -l $pexfile
will show you the contents
a
It has all the files correctly
w
do they have relative
__init__.py
files? pex will create some
a
it's working fine if
Copy code
import module
modeul.something()
w
also, do you have any overlapping packages? if so, you might need to declare them
a
importlib.import_module('module')
fails
I do have an empty
__init__.py
under cloudwatch_to_tsd directory
w
are any of your packages overlapping?
ie, declared in multiple directories?
a
I don't think so
w
for example, pants splits the
pants.contrib
package across a bunch of directories, and then they are merged
k. if you could open a ticket with a repro of this, somebody might be able to make some progress
a
OK. Will do thanks.