Importing other python packages in __init__.py - python

I would like to know if importing packages like pandas or NumPy, which can be the basic need for many modules of a package, should be imported in init.py? If not, can anyone please tell how they should be imported, rather than importing them in each module of a package?

No, you should import them in each python file you need them in.
The init is used, when you want to use your module in another project and want want to make the modules classes, functions, etc. available for outside the module

Related

How to import packages from inside another package

So, I am making an application in python where i am inside a package, and i want to a import a package from outside that older. I have tried os.chdir() but that doesn't work. I have tried to import it by simply saying
I expect the package to be imported and any file that i want can be used as many times as possible so that i can have a more structed program, and easier to understand
My username is too new to comment so I'll indirectly answer, if your UserData is a module (given there is an __init__), and so too is SignLoginLogic, and they are at the same directory level, I have never seen a case where you're importing using your requested method (importing somehow from another module's __init__.py directly). See https://docs.python.org/3/reference/import.html. You should be importing SignLoginLogic into your module using something like from SignLoginLogic import WhatYouWantFromIt or otherwise a relative import.

How to add a module to Python's list of globally importable libraries

I'm trying to work in an environment where I cannot install packages normally using Pip. As such, I need to bundle all my program's dependencies alongside the full program. This works great for simpler dependencies, but for ones that have their own dependencies, imports fail due to their dependencies not being globally available (since neither of them can be imported using absolute imports).
Currently, my modules are as follows:
main.py (uses my_module)
my_module/ (depends on foo)
somefile.py
anotherfile.py
foo/ (depends on bar)
# Contents of third-party module
bar/
# Contents of other third-party module
Is there a way that I can alias a module that I have imported relatively, so that whenever some other module tries to import it, it will be referred to the place where I have directed it? I'd much rather do that than have to risk modifying thousands of lines of unfamiliar code to make all the import statements relative.
One way is to create a custom pip package as explained here:
Use custom Python package

What is happening during module/package imports

Can anyone help me understand what happens behind the scenes when I import a module in Python? Is there any memory or processing overhead associated with importing? Or is it more like opening a door to extend the local namespace? I don't really need specifics, just a general idea.
I'm also hoping to understand what happens when a full package is imported, when there is no __init__.py inside the package folder (only Python 3+, I think).
Also, what changes when that package is imported into a module that is part of the package being imported? Does Python automate the __init__.py based on where the package is being imported? If it does do something smart like this, does that mean it is actually better to avoid writing the package __init__.py?
My main reason for asking these questions is to know what I should and shouldn't do as far as importing goes. It seems convenient to just import a package and use whatever I want from it without worrying about which specific modules I need. But I don't know the full impact of doing something like this.
Edit: I just wanted to add the important detail that my modules are all already loaded into memory.

How to use trace module to determine all the imported modules on executionpath?

In the official document said the following code
python -m trace --count -C . somefile.py ...
lists all the modules imported during the execution.
However, I couldn't find any documentation about the programmatic API for doing the same thing inside the code.
What I am looking for is not How to list imported Modules?. My concern is listing all the imported modules and imported modules of those imported modules and so on, If and only if for any particular module it is imported on execution path.
If you are looking for which modules are imported at a given time in the environment, then you can use the sys.modules dictionary. There is also a sys.path list, which shows in which directories the interpreter is looking for python modules.
If you need a list of modules being imported by a particular module, then you have to parse the import statements and build a list yourself, recursively (much more work, but doable).

Organize a python library as plugins

I would like to create a library, say foolib, but to keep different subpackages separated, so to have barmodule, bazmodule, all under the same foolib main package. In other words, I want the client code to be able to do
import foolib.barmodule
import foolib.bazmodule
but to distribute barmodule and bazmodule as two independent entities. Replace module with package as well... ba[rz]module can be a fukll fledged library with complex content.
The reason behind this choice is manifold:
I would like a user to install only barmodule if he needs so.
I would like to keep the modules relatively independent and lightweight.
but I would like to keep them under a common namespace.
jQuery has a similar structure with the plugins.
Is it feasible in python with the standard setuptools and install procedure ?
You may be looking for namespace packages. See also PEP 382.
Yes, simply create a foolib directory, add an __init__.py to it, and make each sub-module a .py file.
/foolib
barmodule.py
bazmodule.py
then you can import them like so:
from foolib import barmodule
barmodule.some_function()

Categories

Resources