guidelines for importing modules in python - python

I know that there have been a number of questions concerning importing modules in python, but my question seems to be somewhat different.
I'm trying to understand when you have to import a whole module as opposed to when you have to import a specific entry in the module. It seems that only one of the two ways work.
For example if I want to use basename, importing os.path doesn't do the trick.
>>> import os.path
>>> basename('scripts/cy.py')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'basename' is not defined
Instead I need to import basename from os.path as in
>>> from os.path import basename
>>> basename('scripts/cy.py')
'cy.py'
Going the other way, if I want to use shutil.copyfile, importing copyfile from shutil doesn't work
>>>
>>> from shutil import copyfile
>>>
>>> shutil.copyfile('emma','newemma')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'shutil' is not defined
Instead I must import shutil as in
>>>
>>> import shutil
>>>
>>> shutil.copyfile('emma','newemma')
'newemma'
>>>
The only way I have been able to get this right is through experimentation.
Are there some guidelines to avoid experimentation?

If you import
import os.path
then you have to use full namespace os.path
os.path.basename()
If you import with from
from shutil import copyfile
then you don't have to use full namespace shutil
copyfile(...)
That's all.
If you use as
import os.path as xxx
then you have to use xxx instead of os.path
xxx.basename()
If you use from and as
from os.path import basename as xxx
then you have to use xxx instead of basename
xxx()

you could use form module import *
from time import *
sleep(2)
which allow you to call its submodules, instead of:
from time import sleep
sleep(2)
or:
import time
time.sleep(2)
this imports every sub-module of the package
https://docs.python.org/2/tutorial/modules.html#importing-from-a-package

Related

Executing external python script function

I have two scripts in the same folder:
5057_Basic_Flow_Acquire.xyzpy
5006_Basic_Flow_Execute.xyzpy
I need to call function run() from 5057_Basic_Flow_Acquire file in the 5006_Basic_Flow_Execute file.
I tried two approaches:
1.
import time
import os
import sys
import types
dir_path = os.path.dirname(os.path.realpath(__file__))
#os.chdir(str(dir_path))
sys.path.append(str(dir_path))
sensor = __import__('5057_Basic_Flow_Acquire')
import time
import os
import sys
import types
import importlib
import importlib.util
dir_path = os.path.dirname(os.path.realpath(__file__))
spec = importlib.util.spec_from_file_location('run', dir_path+'\\5057_Basic_Flow_Acquire')
module = importlib.util.module_from_spec(spec)
Pycharm is reporting this type of errors:
case:
ModuleNotFoundError: No module named '5057_Basic_Flow_Acquire'
case
AttributeError: 'NoneType' object has no attribute 'loader'
So in both cases module was not found.
Does someone have any suggestion?
You should rename your files to something like :
5057_Basic_Flow_Acquire_xyz.py
5006_Basic_Flow_Execute_xyz.py
so that they are recognized as modules and can be imported.
Then, in your 5057_Basic_Flow_Acquire_xyz module, you can import the run function with the following line :
from 5006_Basic_Flow_Execute_xyz import run
Both files are in the same directory, so you should be able to import without changing your PATH environment variable as the current directory is automatically added at the top of the list.

Nested import between ipynb files doesn't import properly

I am using import_ipynb module to import one jupyter notebook inside another.
txt2csv.ipynb has the following import statements
import pandas as pd
import os
import import_ipynb
import config as cfg
I am using another file to import txt2csv.ipynb which has following import statements
data_processing.ipynb
import glob
import numpy as np
import datetime
import import_ipynb
import txt2csv
My issue is that when I try to use a statement like
data_in = glob.glob(os.path.join(cfg.data_dir, "*.csv"))
inside data_processing.ipynb it throws the error
NameError Traceback (most recent call last)
<ipython-input-2-683b1d3df82f> in <module>
----> 1 data_in = glob.glob(os.path.join(cfg.data_dir, "*.csv"))
NameError: name 'os' is not defined
Are nested imports not possible in python, will I have to explicitly write import os again in data_processing.ipynb file?

importing the right module but getting an error in python

import sys
from subprocess import run, PIPE
import shlex
from src.detector_main import detect_main
def main():
# print command line arguments
for arg in sys.argv[1:]:
print(arg)
if __name__ == "__main__":
# main()
print(sys.argv)
This is my main module. If you see the from src.detector_main import detect_main, it is supposed to import detect_main from src/detector_main.py.
In my detector_main.py, I have a bunch of imports,
import ast
import os
import fpdf
import sys
from Detector.class_coupling_detector import detect_class_cohesion
from Detector.cyclomatic_complexity_detector import detect_cyclomatic_complexity
from Detector.long_lambda_detector import detect_long_lambda
from Detector.long_list_comp_detector import detect_long_list_comp
from Detector.pylint_output_detector import detect_pylint_output
from Detector.shotgun_surgery_detector import detect_shotgun_surgery
from Detector.useless_exception_detector import detect_useless_exception
# from tools.viz_generator import add_viz
def detect_main(directory):
# Get stats for files in directory
stats_dict = get_stats(directory)
....
Running my main module gives me this error:
File "pyscent.py", line 5, in <module>
from src.detector_main import detect_main
File "C:\Users\user\Desktop\proj\src\detector_main.py", line 5, in <module>
from Detector.class_coupling_detector import detect_class_cohesion
ModuleNotFoundError: No module named 'Detector'
I don't get this because I am following the exact path.
I don't get this because I am following the right path.
In your example you import the Detector.class_coupling_detector module in file that is in the same directory as Detector but your cwd is not the src directory.
Due to this you should either use absolute import from src.Detector... or relative import from .Detector...
Here is some information about difference between this two ways to import

Have problems in importing classes from another file in parent folder

The files I need in the directory look like
face/util/load_data.py
face/preprocessing.py
preprocessing.py defines the classes FaceDetector, FaceAligner and the method clip_to_range
I want to import these classes into load_data.py
I am trying to execute this statement inside load_data.py
from preprocessing import FaceDetector, FaceAligner, clip_to_range
I am getting the error
Traceback (most recent call last):
File "utils/load_data.py", line 7, in <module>
from preprocessing import FaceDetector, FaceAligner, clip_to_range
ImportError: cannot import name 'FaceDetector'
Can you please tell me how to correctly import these classes?
You can try adding __init__.py
Which will allow you to import face module, then you can import like,
from face.preprocessing import FaceDetector, FaceAligner, clip_to_range
Update:
Another way is to insert module in sys.path,
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
import preprocessing
move preprocessing.py to util directory.
Look for the file in the path of the executed file.
If you do not want to change the directory structure. Add the following code.
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
Import is possible regardless of directory structure.
You will can solved.

How to change built_in module in python

I want to change os.path in os.py, but it failed. path is different in different platform.
os.py
import ntpath as path
sys.modules['os.path'] = path
from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep, devnull)
It turns out that
from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
devnull)
ImportError: No module named path
Your approach should work. Rename the subdirectory os in your current directory to my_os. Python finds your os directory first and tries to import from there.
Adding this line:
__future__ import absolute_import
to the beginning of the os.py avoids this problem by using absolute imports.
did you try with "__import__" function ?
import mtpath as path
os_path = __import__(path, globals(), locals(), ['curdir', 'pardir', 'sep', 'pathsep', 'defpath', 'extsep', 'altsep', 'devnull']
Then, you can use 'curdir' as :
os_path.curdir
Well, you can also asign it to 'curdir' name as in the documentation :
curdir = os_path.curdir
pardir = os_path.curdir
…

Categories