https://pantsbuild.org/ logo
a

ancient-petabyte-32039

03/14/2019, 7:41 PM
I wanto dynamically import
module
from app.py and it's getting no module found error
w

witty-crayon-22786

03/14/2019, 7:45 PM
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

ancient-petabyte-32039

03/14/2019, 7:46 PM
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

witty-crayon-22786

03/14/2019, 7:47 PM
where is it located?
a

ancient-petabyte-32039

03/14/2019, 7:47 PM
it only has dependencies to 3rd party libraray
under cloudwatch_to_tsd which is
package1
in my example
w

witty-crayon-22786

03/14/2019, 7:47 PM
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

ancient-petabyte-32039

03/14/2019, 7:48 PM
Copy code
-src
 - main
   - python
    - company
     - cloudwatch_to_tsd
       - BUILD
       - app.py
       - module.py
w

witty-crayon-22786

03/14/2019, 7:48 PM
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

ancient-petabyte-32039

03/14/2019, 7:49 PM
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

witty-crayon-22786

03/14/2019, 8:02 PM
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

ancient-petabyte-32039

03/14/2019, 8:04 PM
It has all the files correctly
w

witty-crayon-22786

03/14/2019, 8:06 PM
do they have relative
__init__.py
files? pex will create some
a

ancient-petabyte-32039

03/14/2019, 8:06 PM
it's working fine if
Copy code
import module
modeul.something()
w

witty-crayon-22786

03/14/2019, 8:07 PM
also, do you have any overlapping packages? if so, you might need to declare them
a

ancient-petabyte-32039

03/14/2019, 8:07 PM
importlib.import_module('module')
fails
I do have an empty
__init__.py
under cloudwatch_to_tsd directory
w

witty-crayon-22786

03/14/2019, 8:08 PM
are any of your packages overlapping?
ie, declared in multiple directories?
a

ancient-petabyte-32039

03/14/2019, 8:08 PM
I don't think so
w

witty-crayon-22786

03/14/2019, 8:08 PM
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

ancient-petabyte-32039

03/14/2019, 8:09 PM
OK. Will do thanks.
3 Views