Importing library once for a python module

I want to structure imports of my python module/package, namely I have several *.py files in my module. All of them use:

 import numpy as np

in some pf them I use:

import pandas as pd

can I set the global import for my python module, and say that it uses numpy as np in all the *.py files of the module. I tried something in __init__.py but it didn't work as expected. Is it anyhow reasonable to make global imports?

Upvotes: 0

Views: 64

Answers (2)

Himanshu Bansal
Himanshu Bansal

Reputation: 486

Warning:- Do not use this at home.Not a good practice
You can Do it by importing all your libraries into a single file. For example:-

library.py

import numpy as np
import os
import json
import pandas as pd

And then import this file in your code files

main.py

from library import *

a = np.array([1, 2, 3])

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599490

No, you cannot do this, it is fundamentally opposed to the way Python works.

Upvotes: 2

Related Questions