0

Can anyone help me with a question, if a python package is imported can we directly import package inside another package in package?

Lets say a python pkg structure is like:

   module
   |__ module1
   |   |__ __init__.py
   |   |__ pyScript1.py
   |   |__ pyScript2.py
   |__ __init__.py

Then is a python script if we have a script like import module Can we call the pyScript1 using module.pyScript1 without importing module1?

I know this will work by adding all in module > init.py file and add the module1 sub packages in it. but when I run dir(module) I can see module1 in the list. But I don't want to see module1 in dir list.

Can anyone help with this?

EDIT:

What if instead of 'module1' if we have multiple version modules in 'module' like '1_0_0, 1_1_0, 2_0_0,...etc' and we maintain and environmental variable MODULE_VERSION=1.1.0 and according to the version specified we need to refer to that particular version module.

  • Does this answer your question? [Import a file from a subdirectory?](https://stackoverflow.com/questions/1260792/import-a-file-from-a-subdirectory) – sahasrara62 Jul 10 '20 at 11:27

2 Answers2

0

Then is a python script if we have a script like import module Can we call the pyScript1 using module.pyScript1 without importing module1?

The answer, is dependent what you have declared to be initialized in the package __init__.py. if you import the pyScript1.py in the __init__.py then the answer yes and if no then the answer is no.

dunder all __all__ :

dunder all is good tool for controlling what to export from our module when we got imported (our module not the package).

Adam
  • 2,820
  • 1
  • 13
  • 33
  • What if we don't have __init__.py file in module package... Currently I'm trying to achive "activating versions for the python packages" Lets say "module" is a package and we will have multiple version for "module" and we will define an environmental variable saying MODULE_VERSION=1.0.0 and the version number will be specified in the module package. Can we import modules like this? – Sunil Kumar Nerella Jul 10 '20 at 10:28
  • what you mean by .."if we don't have init.py file in module package..", you cant import your module or any thing else. python packages is namespace packages. this is a different case and out of you question scope. – Adam Jul 10 '20 at 10:42
  • Sorry, I mean if `__init__.py` is there but version module will be dynamic selected according to the environment variable of the version will be changed. How can we go into particular version module dynamically? – Sunil Kumar Nerella Jul 10 '20 at 11:03
0

inside module/__init__.py file write

from  .module1 import pyScript1 
__all__ =['pyScript1']
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • What if we want to use versions instead of "module1" like "1_0_0". Import line won't work as the module doesn't starts with a character. Let's say that we have an environment variable "module_version=1_0_0" how to get the scripts inside version directory...? – Sunil Kumar Nerella Jul 10 '20 at 11:22
  • i think this is related to your package setup, and you can assign value as `module_name=` plus it seems your practice are off from [pep guidelines](https://www.python.org/dev/peps/pep-0008/), you need to improve the structure or share your setup file for more clearlty – sahasrara62 Jul 10 '20 at 11:26
  • Sorry for in appropriate information. I know that we can't create a module which is starting with numbers. But I'm trying to achieve a way that if we can maintain an environment where other modules may refer the specified version of current module. This way we may work with upgrading the versions and use the python modules. I think you understand the issue – Sunil Kumar Nerella Jul 10 '20 at 11:44
  • 1
    mabye be common folder with the package name and store version in subdirectories, i gues you need to see how pypi maintain it's python modules – sahasrara62 Jul 10 '20 at 12:02