Repository: conan-io/conan

Issue:
Environment for package() function differs when using conan create and conan export-pkg
I'm trying to reuse my python code. For this I've made conan package named `ConanQMakeHelper`. I've made it just like in docks. Now I put this project as a dependency in `requires` varible of conanfile. And I use some functions from this package in `build()` and `package()` methods of third project. Both methods start `from qmakehelper import QMakeHelper`. Now if I use `conan create ...` all is OK. But I'm using CI. And in CI recipe on first stage I use `conan install ...` then `conan build ...` and in build method  `qmakehelper` is visible. But on second CI stage I use `conan export-pkg`, and it fails and says that no `qmakehelper` found. The problem is that `os.environ['PYTHONPATH']` is empty in package method.
`ConanQMakeHelper` is package name, `qmakehelper.py` is the python script file inside, `QMakeHelper` is a class in this file.

```
mkdir build_win
cd build_win
conan install .. -s build_type=Release --build=never -u
conan build -bf . -sf .. ..
cd ..
conan export-pkg -bf build_win -if build_win . some/dev --force
```

Conan 1.1.0


Discussion:
Hi @Ujifman 

Thanks for your report. It is possible that the python path is not being correctly applied, I will have a look.

Resolving patch:
diff --git a/conans/client/manager.py b/conans/client/manager.py
--- a/conans/client/manager.py
+++ b/conans/client/manager.py
@@ -172,9 +172,6 @@ def export_pkg(self, reference, source_folder, build_folder, package_folder, ins
 
         loader = self.get_loader(profile)
         conanfile = loader.load_virtual([reference], scope_options=True)
-        if install_folder and existing_info_files(install_folder):
-            _load_deps_info(install_folder, conanfile, required=True)
-
         graph_builder = self._get_graph_builder(loader, remote_proxy)
         deps_graph = graph_builder.load_graph(conanfile, check_updates=False, update=False)
 
@@ -182,6 +179,8 @@ def export_pkg(self, reference, source_folder, build_folder, package_folder, ins
         # one, the first of the first level, always existing
         nodes = deps_graph.direct_requires()
         conanfile = nodes[0].conanfile
+        if install_folder and existing_info_files(install_folder):
+            _load_deps_info(install_folder, conanfile, required=True)
         pkg_id = conanfile.info.package_id()
         self._user_io.out.info("Packaging to %s" % pkg_id)
         pkg_reference = PackageReference(reference, pkg_id)
diff --git a/conans/client/tools/env.py b/conans/client/tools/env.py
--- a/conans/client/tools/env.py
+++ b/conans/client/tools/env.py
@@ -1,8 +1,7 @@
+import os
 import sys
 from contextlib import contextmanager
 
-import os
-
 from conans.client.tools.files import which, _path_equals
 from conans.errors import ConanException
 
@@ -58,7 +57,7 @@ def no_op():
 def remove_from_path(command):
     curpath = os.getenv("PATH")
     first_it = True
-    for n in range(30):
+    for _ in range(30):
         if not first_it:
             with environment_append({"PATH": curpath}):
                 the_command = which(command)
@@ -75,10 +74,11 @@ def remove_from_path(command):
 
         curpath = os.pathsep.join(new_path)
     else:
-
         raise ConanException("Error in tools.remove_from_path!! couldn't remove the tool '%s' "
-                             "from the path after 30 attempts, still found in '%s' this is a Conan client bug, please open an issue at: "
-                             "https://github.com/conan-io/conan\n\nPATH=%s" % (command, the_command, os.getenv("PATH")))
+                             "from the path after 30 attempts, still found in '%s' this is a "
+                             "Conan client bug, please open an issue at: "
+                             "https://github.com/conan-io/conan\n\nPATH=%s"
+                             % (command, the_command, os.getenv("PATH")))
 
     with environment_append({"PATH": curpath}):
         yield


Test patch:
diff --git a/conans/test/integration/python_build_test.py b/conans/test/integration/python_build_test.py
--- a/conans/test/integration/python_build_test.py
+++ b/conans/test/integration/python_build_test.py
@@ -137,6 +137,10 @@ def reuse_test(self):
         self.assertNotIn("Hello Bar", client.user_io.out)
         self.assertIn("Hello Foo", client.user_io.out)
 
+        client.run("package . -pf=mypkg")
+        self.assertNotIn("Hello Bar", client.user_io.out)
+        self.assertIn("Hello Boom", client.user_io.out)
+
         client.run("export . lasote/stable")
         client.run("install Consumer/0.1@lasote/stable --build")
         lines = [line.split(":")[1] for line in str(client.user_io.out).splitlines()
@@ -144,6 +148,9 @@ def reuse_test(self):
         self.assertEqual([' Hello Baz', ' Hello Foo', ' Hello Boom', ' Hello Bar'],
                          lines)
 
+        client.run("export-pkg . lasote/stable -f")
+        self.assertIn("Hello Boom", client.out)
+
     def upload_reuse_test(self):
         server = TestServer()
         servers = {"default": server}