Import a module function and required dependencies for it - python

How do I import and run a Python function and have all the dependencies it uses use the imports from the main Python file?
Main Python file:
from im import er
import time
er()
Python file with function to be imported:
def er():
time.sleep(1)
print('hi')
This doesn't work because the time module is not imported in the im.py. How can I make this work without importing the modules it needs each time I run the function?

You have to import the function in the main, and required module for the function in the function file.
Main Python file:
from im import er
er()
Imported module :
from time import sleep
def er():
sleep(1)
print('hi')
This behave this way because Python run the imported module when you import it. Then, depending of your import statement, it will do:
import <module>: create a module object named <module> with attributes allowing you to access global scope of this module. If a function is in the global scope of the module, you access it with <module>.<function_name>.
from <module> import *: add to the global scope of the current module the global scope of the imported module (not exactly, but if you need more informations about it, look for wildcard import behaviour). As a good rule of thumb with this import: don't use this.
from <module> import <symbol>: add the symbol of the imported module to the global scope of the current module.
More info about imports:
https://stackoverflow.com/a/10501768/6251742
https://docs.python.org/3/reference/import.html

Related

How to import module contained in my customized module into python interactive environment's namespace?

I write the below module myinit.py:
import os
import numpy as np
import pandas as pd
import datetime
And copy it into search path.
sudo cp init.py /usr/local/lib/python3.5/dist-packages/myinit.py
Now enter into my python's interactive environment with python in cmd.
import myinit
dir(pd)
NameError: name 'pd' is not defined
dir(pandas)
NameError: name 'pandas' is not defined
dir(myinit.pd)
<module 'pandas' from '/usr/local/lib/python3.5/dist-packages/pandas/__init__.py'>
In my current python cmd environment,i have two ways to call pandas.
Call it with myinit.pd
Input import pandas as pd in current python cmd environment,then call it with pd.
Both of them make me uncomfortable,there is no os, np ,pd ,datetime in current python namespace.
how to call module imported by my customized module with itself's name?
import myinit imports the entire myinit module, but it only adds one name to the local namespace: myinit.
Now, myinit may itself contain names like pd, np, os, and datetime (as well as any variables/functions you've defined within it), but they're inside myinit. So, to access them, we need to do
import myinit
dir(myinit.pd)
# <module 'pandas' from '/usr/local/lib/python3.5/dist-packages/pandas/__init__.py'>
Essentially, namespaces are nested like this. We use dot notation to descend from namespace to namespace (e.g. pandas has its own namespace, pandas.DataFrame has its own namespace, and so on down the line).
You can import a specific name from a different module by using from:
from myinit import pd
dir(pd)
<module 'pandas' from '/usr/local/lib/python3.5/dist-packages/pandas/__init__.py'>
dir(os)
# error - we didn't import os, only pd
And the way to import every name contained within a different module into the local namespace, as you seem to be trying to do, is with an asterisk:
from myinit import *
dir(pd)
dir(os)
...
Though you can do this, you shouldn't - per PEP 22, "Explicit is better than implicit", and you should be clear on exactly what names you're using from a given import.
By the way, don't worry about importing the same module twice (like pandas) in two different files. It only ever gets loaded into memory once - the sys module contains a cache of previously-loaded modules throughout the program, and if you try to load a module anywhere in your program that has been loaded before, it just picks the same reference from that cache instead of loading a whole new instance.
Importing pandas, or os, or pretty much any other module, in multiple files in your program, is better code style, as it makes it more clear what tools you're using and where they come from.

Import module, that import another module. Python

I was curious what happens when we import a module that in turn imports another module. So I create two modules: module1 and module2.
module1:
import random
print(random.randint(0,10))
print("module1 work")
module2:
import module1
print("module2 work")
When I run module2 it give me this output:
1
module1 work
module2 work
So, I decided that I did indeed import random, when I imported module1. But when I type in the Shell print(random.randint(0,10))it throws a NameError: name 'random' is not defined. So random wasn't imported from module1. But in this case why did module2 print 1, and didn't throw the same error as the Shell?
Each module has its own scope (or namespace, if that terminology is more familiar to you). If you want to access random from module2, you need to import it in module2. The interpreter shares the scope of the module you execute, so only variables declared in the global namespace of that module will be accessible. If you want to access random from the interpreter having only imported module2, you'll need to specify module1.random.
Alternatively, you can replace import module1 with from module1 import *. That will copy over everything, including the reference to random. So random will be accessible globally.
It is because you are not actually importing random into the shell, instead just the file that contains the module.
We can use an existing module as an example, for example tkinter which opens with:
import enum
import sys
They import into the Tkinter module but when you import Tkinter they don't come with it.
To put it is as simply as possible your module1 has random imported, but imporing module1 will not also import random

Import function from file without child imports

I've created a python module of functions that I have developed. Within this module there are several imports, some of these are not native to python and needed to be installed.
I have one instance where i need a python script to access a function in this module, but i dont want it to try and use all of the other imports that are already in the module. I've created a very basic example of what this setup looks like below.
for example:
#this is the module, named MOD.py
import win32con
def func1():
data = win32con.function()
return data
def func2():
return do_action()
#this is the exterior script
from MOD import func2
data = func2()
Why is it that it will still attempt to import the win32con module within MOD.py even though func2 does not use it? Naturally if the module isn't installed I'll get an ImportError on the win32con line. I don't want to have to install these modules on machines every time i want to run code that does not even use it.
If the import is only used in func1, you could import it within func1:
#this is the module, named MOD.py
def func1():
import win32con
data = win32con.function()
return data
def func2():
return do_action()

Import sys not getting imported when given inside a function [Python]

generic_import.py
def var_import():
import sys
import glob
main.py
from generic_import import *
var_import()
whenever I run the main file, I get
NameError: name 'sys' is not defined
However when import sys is outside the function,the code is executed without any error.
generic_import.py
import sys
def var_import():
import glob
What's the reason behind this ? I want to import it inside the fucnction.
The reason for this is the Scope of the Imports.
You are importing the libraries inside a function so the scope of these is var_import and as soon as the function terminates the scope is discarded. You would need to return the imported libraries and then save them inside the scope you want to use them.
But I would recommend just import libraries as needed without any generic_import functionality.
If you are worried about namespace conflicts: You can always use aliases like import sys as python_builtin_sys but I wouldn't recommend this either.
Since you asked about how to get the modules outside the function scope I'll provide a short example code.
def var_import():
import sys
import glob
return sys, glob
and you can get them into your wanted scope by using something along the lines of:
from generic_import import var_import # Do not use "import *"
sys, glob = var_import()
or something more advanced if you don't know the number of loaded modules.

Importing imports within a function - Python 2.6

I have two files, SysDump.py and libApi.py in the same folder.
In SysDump I do:
from libApi._SysDump import *
In libApi I have:
def _SysDump():
import cPickle as _cPickle
import math as _math
from zipfile import ZipFile as _ZipFile
import re as _re
However I get the error:
from libApi._SysDump import *
ImportError: No module named _SysDump
I use VS2012+PTVS to step through the code and the execution trace goes to def _SysDump() in libApi as I steop through but does not enter it. Question is how do I make this work in Python 2.6 only please?
from libApi._SysDump import *
When writing this, Python looks for a package libApi and a module in it called _SysDump. A package is equivalent to a folder and a module is a single file. From your explanations, this is not the situation you have in your case. You have a module libApi with a function _SysDump. So if anything, you could do this:
from libApi import _SysDump
So you would get a reference to the _SysDump function. Note that running that function will not give you references to all the modules you are trying to import. Inside the function, the modules will be imported and assigned to local variables. After the function ends, those references are gone.
If you want to have some module take care of all your imports, you could make a file that performs those imports and import everything from that module:
# imports.py
import cPickle as _cPickle
import math as _math
from zipfile import ZipFile as _ZipFile
import re as _re
And then:
from imports import *

Categories