Centralized Python Logging - Unable to import logging file - python

I found an example online of a config file to build on the base logging and colorlog.
File Structure
main.py
directory.py
client.py
/utils/log.py
main, directory and client all have from utils.log import init_logger which is the function inside my log.py.
For processing,
main.py imports a function from directory.py which imports a function from client.py.
VSCODE complains that the modules can't be imported, but the script runs fine with no errors except vscode complaining.
I think I have a circular dependency issue going on, but can't figure out how I should share the logging config between all my files without importing it.
main.py
"""
Setup Trusts Between Directories
"""
import json
from utils.log import init_logger
from directory import stage
###############
def main():
"""
MAIN TRUST FUNCTION
"""
## Initialize Logger
if __name__ == "__main__":
logger = init_logger(__name__, testing_mode=False)
logger.info("Running Creator")
## Get Outputs
new_dirs = get_output("newDirectoryIds")
config_dirs = get_output("configDirectories")
## Get Directory Stage
stage(new_dirs, config_dirs)
###############
## Run Main Function
main()
directory.py
"""
directory.py
- Loops Through List of Directories
- Returns back when stage is Active or Failed
"""
import time
from utils.log import init_logger
from get_client import get_new_client
###########
def stage(new_dirs, config_dirs):
"""
Returns Directory Stage Value
- Loops until Directory is Failed or Active
"""
## Initialize Logger
logger = init_logger(__name__, testing_mode=True)
for config_dir in config_dirs:
## Get Describe Directory Services Client
ds_client = get_new_client(config_dir["account"], "ds", "us-west-
2")
## Get All Directories in Account
temp_dirs = ds_client.describe_directories()

This is what worked for me per Pylint "unresolved import" error in visual studio code
"The best solution for now is to create a .env file in your project root folder. Then add a PYTHONPATH to it like this:"
PYTHONPATH=YOUR/MODULES/PATH
and in your settings.json add
"python.envFile": ".env"

Related

Import variables from a config File

I have a script that have multiple files and a config file with all the variables store in one Python file.
Folder structure:
Config file:
If I try to run the main file which calls the head function imported, an error pops up saying that the config cannot be imported.
Imports:
Your Functions folder has a __init__.py file. If your app executes from Main.py (ie if Main.py satisfies __name__ == "__main__") therefore wherever you are in your app you could import the config like this:
from Functions.Config import *
Edit:
However,from module import * is not recommended with local import. You could give an alias to the import and call name.variable instead of calling name directly.
Example:
def head():
# import packages
import Function.Config as conf
print(conf.more_200)
head()
>>> 50
Your syntax are wrong.
It is supposed to be from Config import * and not import .Config import *

Unable to import a function from outside the current working directory

I am currently having difficulties import some functions which are located in a python file which is in the parent directory of the working directory of the main flask application script. Here's how the structure looks like
project_folder
- public
--app.py
-scripts.py
here's a replica code for app.py:
def some_function():
from scripts import func_one, func_two
func_one()
func_two()
print('done')
if __name__ == "__main__":
some_function()
scripts.py contain the function as such:
def func_one():
print('function one successfully imported')
def func_two():
print('function two successfully imported')
What is the pythonic way of importing those functions in my app.py?
Precede it with a dot so that it searches the current directory (project_folder) instead of your python path:
from .scripts import func_one, func_two
The details of relative imports are described in PEP 328
Edit: I assumed you were working with a package. Consider adding an __init__.py file.
Anyways, you can import anything in python by altering the system path:
import sys
sys.path.append("/path/to/directory")
from x import y
1.
import importlib.util
def loadbasic():
spec = importlib.util.spec_from_file_location("basic", os.path.join(os.path.split(__file__)[0], 'basic.py'))
basic = importlib.util.module_from_spec(spec)
spec.loader.exec_module(basic)
return basic #returns a module
Or create an empty file __init__.py in the directory.
And
do not pollute your path with appends.

Can't find module when loading Jupyter Server Extension

I want to load a Jupyter Notebook Server Extension within a local directory:
server_ext/
|__ __init__.py
|__ extension.py
extension.py
from notebook.utils import url_path_join
from notebook.base.handlers import IPythonHandler
class HelloWorldHandler(IPythonHandler):
def get(self):
self.finish('Hello, world!')
def load_jupyter_server_extension(nbapp):
"""
nbapp is istance of Jupyter.notebook.notebookapp.NotebookApp
nbapp.web_app is isntance of tornado.web.Application - can register new tornado.web.RequestHandlers
to extend API backend.
"""
nbapp.log.info('My Extension Loaded')
web_app = nbapp.web_app
host_pattern = '.*$'
route_pattern = url_path_join(web_app.settings['base_url'], '/hello')
web_app.add_handlers(host_pattern, [(route_pattern, HelloWorldHandler)])
I run the following command from the directory containing server_ext:
jupyter notebook --NotebookApp.server_extensions="['server_ext.extension']"
But I get the error "No module named extension". Is there something I have to do to get Jupyter/python session to recognize the path to the module?
Figured it out-
it turns out that Jupyter Notebook's call to importlib.import_module sets package=None, which means that relative paths will not work.
As a workaround, the ~/.jupyter/jupyter_notebook_config.py script can be modified to append your local directory to the PYTHONPATH so that the module can be found.
import sys
sys.path.append("C:\\Users\\eric\\server_ext")
c.NotebookApp.server_extensions = [
'extension'
]

How to import a function which is in a different directory

I am trying to calling a function from a module which is located in different directory than the current.
This is code I am using
c:\commands contains a file nlog.py which contains function parselog . i would like to importing this function
into a python file in other directory c:\applications
def parselog(inFileDir = )
### This is in directory c:\commands which need to imported ####
c:\applications\pscan.py is the script which is calling / importing from the above file / directory
if __name__ == '__main__':
#sys.path.append("C:/commands")
sys.path.insert(0,'C:/commands')
from commands import nlog
def pscan (infileDir = )
parselog(inFileDir=r'os.getcwd()') # I am calling the function here
We are getting error
NameError: global name 'parselog' is not defined
I am not sure if I am importing in a wrong way. The C:\commands folder contains _init_.py file. Please let me know what I am missing.
Make and empty __init__.py file in the folder of the module you want to import.
Below code should work.
import sys
sys.path.append(r'c:\commands')
import nlog
parselog(inFileDir=r'os.getcwd()')

Python - create object of class from one package in different package

I started using Python few days back and I think I have a very basic question where I am stuck. Maybe I am not doing it correctly in Python so wanted some advice from the experts:
I have a config.cfg & a class test in one package lib as follows:
myProj/lib/pkg1/config.cfg
[api_config]
url = https://someapi.com/v1/
username=sumitk
myProj/lib/pkg1/test.py
class test(object):
def __init__(self, **kwargs):
config = ConfigParser.ConfigParser()
config.read('config.cfg')
print config.get('api_config', 'username')
#just printing here but will be using this as a class variable
def some other foos()..
Now I want to create an object of test in some other module in a different package
myProj/example/useTest.py
from lib.pkg1.test import test
def temp(a, b, c):
var = test()
def main():
temp("","","")
if __name__ == '__main__':
main()
Running useTest.py is giving me error:
...
print config.get('api_config', 'username')
File "C:\Python27\lib\ConfigParser.py", line 607, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'api_config'
Now if I place thie useTest.py in the same package it runs perfectly fine:
myProj/lib/pkg1/useTest.py
myProj/lib/pkg1/test.py
myProj/lib/pkg1/config.cfg
I guess there is some very basic package access concept in Python that I am not aware of or is there something I am doing wrong here?
The issue here is that you have a different working directory depending on which module is your main script. You can check the working directory by adding the following lines to the top of each script:
import os
print os.getcwd()
Because you just provide 'config.cfg' as your file name, it will attempt to find that file inside of the working directory.
To fix this, give an absolute path to your config file.
You should be able to figure out the absolute path with the following method since you know that config.cfg and test.py are in the same directory:
# inside of test.py
import os
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'config.cfg')

Categories