Importing library once for a python module - python

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?

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

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])

Related

Cannot import modules from different folders

For a project I have a script that has to import a module from a different folder.
My file structure is the following:
The code in script.py is the following:
import pandas as pd
import numpy as np
from algorithms.kmeans.kmeans import *
representatives = ["WestVlaanderen", "Hainaut", "Brussels", "BrabantWallon"]
representatives_info = pd.read_csv("data\\filtered_data\KMEANS.csv")
representatives_info = representatives_info[representatives_info["PROVINCE"].isin(representatives)]
representatives_info = representatives_info[["INFECTION_RATE", "HOSPITALISATION_RATE", "TEST_POS_PERCENTAGE"]].to_numpy()
kmeans = Kmeans("data\filtered_data\KMEANS.csv", 4, representatives_info)
kmeans.start_clustering()
Both the folder algorithms and kmeans both have their own init.py file (empty but I read that this doesn't matter). And I still can't import the wanted file.
The error I get is:
No module named 'algorithms'
When I try using the same import on the python interactive mode in my bash terminal it works without errors. I have no idea what I'm doing wrong for the import not to work

Import all packages in main.py

I'd like to import all my packages in only one file.
Let assume that I have a main.py file where I call all my class (from others .py files located in a src folder):
main.py
|-- src
|-- package1.py
|-- package2.py
the main.py looks like this:
from src.package1 import *
from src.package2 import *
def main():
class1 = ClassFromPackage1()
class2 = ClassFromPackage2()
if __name__ == '__main__':
main()
in package1.py I import let say numpy, scipy and pandas
import numpy
import scipy
import pandas
class ClassFromPackage1():
# Do stuff using numpy, scipy and pandas
and in package2.py I use numpy and scikit learn:
import numpy
import sklearn
class ClassFromPackage2():
# Do stuff using numpy and sklearn
Is there a way to import all packages in one file Foo.py where I only write:
import numpy
import sklearn
import scipy
import pandas
and import this Foo.py in src .py? like this for example with package1.py
import Foo
class ClassFromPackage1():
# Do stuff using numpy, scipy and pandas
Is this a good idea? Does it reduce memory consumption? Will it helps python to start the main.py faster?
Looks like you want to make code cleaner? What you can do is create a file like foo.py and put all imports in it. Then you can import modules inside foo.py by doing
from foo import *
This will indirectly import all modules.
The way you have already done it is how it is usually done. Similar to header files in C/C++, you make the dependencies explicit. And that it is a good thing.
You asked if it will run faster, the answer is no. All imports are shared. This, sometimes, causes unwanted side effects, but that is not the question here.

Importing multiple packages macro in Python

Most of my jupyter notebooks usually begin with a long list of common packages to import. E.g.,
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
...
Is there any way I can call all of the same packages using a function defined in another python file?
For example, I tried putting the following in util.py:
def import_usual_packages():
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
And in the main notebook:
import util
util.import_usual_packages()
so that the end effect is I can still call the usual packages without using additional namespaces, e.g.
pd.DataFrame()
At the moment this wouldn't work with what I have above. E.g. pd isn't defined.
EDIT: It's similar but not exactly the same as other questions asking about how to do a from util import *. I'm trying to put the import statements inside functions of the utility python file, not simply at the top of the file. I'm aware that I can simply put e.g. import pandas as pd at the top of the util file and then run from util import *, but that is not what I'm looking for. Putting it within functions gives me additional control. For example, I can have 2 different functions called import_usual_packages() and import_plotting_packages() within the same util file that calls different groups of packages, so that in my notebook I can simply call
import_usual_packages()
import_plotting_packages()
instead of having 10+ lines calling the same stuff everytime. This is purely for personal use so I don't care about if other people don't understand what's going on (in fact, that might be a good thing in some circumstances).
With some slight modifications your method can work. In util.py
def import_usual_packages():
global pd, np # Make the names pd & np global
import pandas as pd
import numpy as np
And in main.py
import utils
utils.import_usual_packages()
utils.pd.DataFrame() # access via the utils namespace
This is definitely not the cleanest approach though.

Import python packages like NumPy or Pandas in imported modules

I have a main Python-program called mainProgram.py. It looks like this (simplified example):
import numpy as np
import pandas as pd
from packOne import functionOne
from packTwo import functionTwo
ResultOne = functionOne()
ResultTwo = functionTwo()
File __init__.py was also created.
After running the main program mainProgram.py I received NameError:
NameError: name 'np' is not defined
In package PackOne.py I use some functions from NumPy, but I didn't import NumPy in the package PackOne.py. Should I also import NumPy in packOne and all other packages which I'm going to import? Are there any elegant solutions, how to import packages like NumPy or Pandas once in main python program?

Python, Function containing imports

I would like to create a function that contains all my imports statement, :
def imports():
import pandas as pd
import numpy as np
etc...
save it in a .py file as a module and call that function from my Jupyter Notebook.
This is simply to unclutter the Notebook. However it seems it doesnt work to create a function containing import statements? (I'm receiving errors NameError: name 'pd' is not defined ). Anyone knows why?
Instead, put into your module all the import statements you want and as you'd normally put them
contents_of_your_module.py
import pandas as pd
import numpy as np
import itertools
import seaborn as sns
Then you import from Jupyter
from contents_of_your_module import *
Or, you can create a namespace for your module and do this
import contents_of_you_module as radar
And then you can access all the modules via your name space
radar.pd.DataFrame
import pandas as pd creates a variable pd. In your case, the variable is local to the function and not visible outside. Declare the shortcuts to the imported modules as global variables:
def imports():
global pd, np
import pandas as pd
import numpy as np
In general, I would say that it's a very bad practice to do what you want to do. If you want to do exactly what you describe you can't do it in a pythonic way.
But this is a solution that probably would work:
Pretend you have a module called imports.py, that should handle all your imports:
File: imports.py:
import numpy as np
import pandas as pd
The you can have a function in your program.
def do_funky_import_from():
from imports import *

Categories

Resources