Conan: Python reuse code in package_info() method fails importing module

Created on 20 Mar 2018  路  8Comments  路  Source: conan-io/conan

To help us debug your issue please explain:

  • [x] I've read the CONTRIBUTING guide.
  • [x] I've specified the Conan version, operating system version and any tool that can be relevant:
    python 3.6.4, conan 1.1.1
  • [x] I've explained the steps to reproduce the error or the motivation/use case of the question/suggestion.

conanfile_HelloPythonConan.py

from conans import ConanFile


class HelloPythonConan(ConanFile):
    name = "HelloPythonConan"
    version = "0.1"
    exports = "__init__.py", "hello.py"
    build_policy = "missing"

    def package(self):
        self.copy('*.py')

    def package_info(self):
        self.env_info.PYTHONPATH.append(self.package_folder)

hello.py

def hello():
    print("Hello World from Python!")

__init__.py


conanfile_HelloPythonReuseConan.py

from conans import ConanFile
import os


class HelloPythonReuseConan(ConanFile):
    name = "HelloPythonReuseConan"
    version = "0.1"
    requires = "HelloPythonConan/0.1@conan/testing"

    def package_info(self):
        from hello import hello
        hello()

Steps to reproduce:

$ conan export conanfile_HelloPythonConan.py conan/testing
$ conan create conanfile_HelloPythonReuseConan.py conan/testing
ERROR: HelloPythonReuseConan/0.1@conan/testing: Error in package_info() method, line 16
        from hello import hello
        ModuleNotFoundError: No module named 'hello'

cc/ @kheaactua

bug

Most helpful comment

Good question... I think the tools.pythonpath() context might be cleaner, as it clears the system env after finishing, while the addsitedir() migth permanently pollute it.

All 8 comments

I have excatly the same problem.

It's probably obvious, but just in case someone is having trouble and finds this issue: In the meantime, I've been getting around this with:

def pacakge_info(self):
   import site; site.addsitedir(self.deps_cpp_info['helpers'].rootpath) # Compensate for #2644
   import my_python_module

   # ...

Yes, also the old-style:

def package_info(self):
        with tools.pythonpath(self):
            import mytest
            mytest.bar(self.output)

@memsharded should we use the with tools.pythonpath(self) approach or the one from @kheaactua ?

Good question... I think the tools.pythonpath() context might be cleaner, as it clears the system env after finishing, while the addsitedir() migth permanently pollute it.

@tonka3000 Yeah, I definitely agree that @memsharded 's solution is superior to what I posted. :) I wasn't aware of tools.pythonpath

Interestingly is that i have only problems if the package (with the package_info import) is a indirect dependency. If it is a direct dependency the auto pythonpath works. @kheaactua do you have any troubles with the build or source function?

Hey @tonka3000, nah, my build/source functions were fine, it was strictly the package_info function that I had issue with. In package_info my PYTHONPATH was blank, so I had issues with every (direct or indirect) dependency.

Was this page helpful?
0 / 5 - 0 ratings