0

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


mbspark
  • 534
  • 3
  • 18
  • Where is the `__init__.py`? You must have __init__.py to other python files no that is the python file. It must look like this: `main.py`, `directory.py`, `client.py`, `__init__.py`, `/utils/log.py`, `/utils/__init__.py`. – ChickenMinh Feb 17 '20 at 06:02
  • I do have an empty `__init__.py` in both the root and utils directory – mbspark Feb 17 '20 at 06:10
  • Can you provide the code which using to import `directory.py` – ChickenMinh Feb 17 '20 at 06:18
  • edited main question with file code from `main.py` and `directory.py` – mbspark Feb 17 '20 at 06:25
  • Can you provide me the error code! Have you created enough 2 `__init__.py`? I see that the `stage` function required 3 arguments, but the `from directory import stage` has no argument. Also it must `"""Setup Trusts Between Directories"""` not `""Setup Trusts Between Directories"""`, do you wrong it in the original code? – ChickenMinh Feb 17 '20 at 06:35
  • I have 1 `__init__.py` in the root directory and 1 `__init__.py` in the `utils` directory. I've corrected the code above. I am passing the right amount of arguments. I edited my code for pasting here. Error code is in vscode as simply `Unable to import 'utils.log'pylint(import-error)` – mbspark Feb 17 '20 at 06:38
  • Change the `PYTHONPATH` environment variable to include the directory above your module. – ChickenMinh Feb 17 '20 at 06:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207957/discussion-between-mbspark-and-ethical-hacker-minh). – mbspark Feb 17 '20 at 06:55
  • I don't like that :) I think [this](https://stackoverflow.com/questions/1899436/pylint-unable-to-import-error-how-to-set-pythonpath) might reduce our chatting time – ChickenMinh Feb 17 '20 at 06:58
  • I changed `log.py` to be in the same directory and still get the error – mbspark Feb 17 '20 at 07:02
  • In my [link](https://stackoverflow.com/questions/1899436/pylint-unable-to-import-error-how-to-set-pythonpath), there are 2 solutions, have you try the second? – ChickenMinh Feb 17 '20 at 07:04
  • yea, no luck. I will try again in the morning :) – mbspark Feb 17 '20 at 07:26
  • You were correct in that the issue was with my IDE trying to locate the right path to the modules. Once I knew the route to go down, I found the solution that was easiest and efficient for me. Thanks! – mbspark Feb 17 '20 at 16:30

1 Answers1

0

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"

mbspark
  • 534
  • 3
  • 18