import ansible.module_utils in 2.2.1.0 as part of inventory module - python

Importing UTILS classes into Inventory - can it be done?
I have created a custom LDAP data importer as part of creating my inventory class. The LDAP schema we have wasn't similar enough to the LDAP plugin provided in samples.
My class is called ldapDataModule; the class is in:
/home/agt/ansible/agt_module_utils/ldapDataModule.py
My "$HOME/.ansible.cfg" file has the following:
module_utils = /home/agt/ansible/agt_module_utils
When running my Ansible inventory module, I get the following output:
ansible ecomtest37 -m ping
ERROR! Attempted to execute "/sites/utils/local/ansible/hosts" as
inventory script: Inventory script (/sites/utils/local/ansible/hosts) had
an execution error: Traceback (most recent call last):
File "/sites/utils/local/ansible/hosts", line 22, in
from ansible.module_utils import ldapDataModule
ImportError: No module named module.utils
The include statement inside hosts appears like this:
import copy
import ldap
import re
import sys
import operator
import os
import argparse
import datetime
import os.path
try:
import json
except:
import simplejson as json
from ansible.module_utils import ldapDataModule
class agtInventory(object):
RECOMENDATIONS?

I was able to do the following as a "work around". I'd still like to hear from Ansible guru's on proper use of "module_utils" variable from ansible.cfg
sys.path.insert(0, '/home/agt/ansible/agt_module_utils')
from ldapDataModule import ldapDataModule

Related

Can't import modules in Python?

I'm following instructions and using files from: https://github.com/eBay/ebay-oauth-python-client
I'm getting error when I import: oauth2api, credentialutil, & model. This is step 3 in the above site.
import yaml, json
sys.path.insert(0, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient/model')
sys.path.insert(1, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/test')
sys.path.insert(2, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient')
import credentialutil
import model
import oauth2api
print(sys.path)
error message:
C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/kyle/PycharmProjects/app/app.py
Traceback (most recent call last):
File "C:/Users/kyle/PycharmProjects/app/app.py", line 10, in
import credentialutil
File "/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient\credentialutil.py", line 20, in
from model.model import environment, credentials
ModuleNotFoundError: No module named 'model.model'; 'model' is not a package
Process finished with exit code 1
The code runs if I only import model:
import yaml, json
sys.path.insert(0, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient/model')
sys.path.insert(1, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/test')
sys.path.insert(2, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient')
import model
print(sys.path)
no error message:
C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/kyle/PycharmProjects/app/app.py
['/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient/model', '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/test', '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient', 'C:\Users\kyle\PycharmProjects\app', 'C:\Users\kyle\PycharmProjects\app', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\python38.zip', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\DLLs', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\lib', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\lib\site-packages', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymodel']
Process finished with exit code 0
I'm also getting a green line under oauthclient, and I don't know why. Everything is spelled correctly.
sys.path.insert(0, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient/model')
sys.path.insert(2, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient')
I can see two problems.
First, it seems that you are using Python under Windows, but you tried to insert a MacOS path to sys.path. Are you sure that paths like /Users/kyle/... really exist in your file system?
Second, you only need to insert the parent path, i.e. /path/to/ebay-oauth-python-client/oauthclient to your sys.path. In my local test, this works:
import yaml, json
import sys
sys.path.insert(0, r"C:\Users\guosh\Downloads\test\ebay-oauth-python-client\oauthclient")
import credentialutil
import model
import oauth2api
print(sys.path)
However, I would suggest you import the package as a whole, like below:
import yaml, json
import sys
sys.path.insert(0, r"C:\Users\guosh\Downloads\test\ebay-oauth-python-client")
import oauthclient
print(sys.path)

Python Paths - ImportError: cannot import name 'dataset_builder'

I am getting an error ImportError: cannot import name 'dataset_builder'. The import command is following:
from object_detection.builders import dataset_builder
The file tree looks following:
-ObjectDetection
-train.py
-models
-research
-object_detection
-builders
- __init__.py
-dataset_builder.py
I am running the train.py from the root directory (os.getcwd() returns following path C:\Users\horakm\PyCharmProjects\ObjectDetection) and I added in the train.py following code to add paths:
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models')
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models\research')
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models\research\slim')
When I print all paths using sys.path i get this:
['C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\python36.zip',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\DLLs',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\win32',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\win32\\lib',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\Pythonwin',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-
packages\\object_detection-0.1-py3.6.egg',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages\\win32',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-
packages\\win32\\lib',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages\\Pythonwin',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models\\research',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models\\research\\slim']
How is it possible that the import statement doesn't work?
Which object are you actually trying to import?
All that are defined in dataset_builder? In that case it should be from object_detection.builders.dataset_builder import *.
Or is the object also called dataset_builder? In that case it is from object_detection.builders.dataset_builder import dataset_builder.

Python cannot import from another file name not defined

I am trying to import a custom function from another python file but keep getting an error NameError: name 'testme' is not defined. I confirmed that I am importing the file correctly according to this SO post and that the function is top level. What else can I try to fix this?
My main python file is:
import sys
import dbconn
#from dbconn import testme #<----did not work
dev=True
if(dev):
categId='528'
pollIds=[529,530,531]
else:
categId=str(sys.argv[1])
pollIds=[529,530,531]
df=testme(categIds)#callServer(categId,pollIds)
df
if(not categId.isdigit):
print('categ id fail. expected digit got: '+categId)
.....
and dbconn.py:
import pymysql #pip3 install PyMySQL
import pandas as pd
from scipy.stats.stats import pearsonr
from scipy import stats
def testme(categIds):
try:
df=categIds
except Exception as e:
print("broke")
return categIds
Not sure if it makes a difference but I am running the main python file from within a Jupyter notebook, and have a compiled version of dbconn.py in the same directory
In response to the suggestions I tried:
df=dbconn.testme(categIds)
got the error:
module 'dbconn' has no attribute 'testme'
You Have to follow these fox exact import
1)import <package>
2)import <module>
3)from <package> import <module or subpackage or object>
4)from <module> import <object>
in your case, you have tried
from dbconn import testme
you have to use only packages in from section and module in import section
like >>
from testme import dbconn

ImportError: cannot import name SlotMap

I am encountering an import error
Traceback (most recent call last):
File "C:\Users\bartis\Desktop\Python\TEC-KB\SlotMapper.pyw", line 9, in <module>
from SlotMapper import SlotMap
File "C:\Users\bartis\Desktop\Python\TEC-KB\SlotMapper.pyw", line 9, in <module>
from SlotMapper import SlotMap
ImportError: cannot import name 'SlotMap
This should be a straightforward issue, but I can’t seem to find the problem. If I place the SlotMapper.py file in the same directory as the GUI I am using the import of SlotMap occurs without error. If I move the file to a directory under the current working directory and add - sys.path.append(os.path.join(os.getcwd(), 'appLib')) I receive the error above. See import statements and modification of PYTHONPATH below. I know the PYTHONPATH has been modified after I checked it from the debugger. I also know since there are other files under appLib required for the GUI to operate. Finally, I have checked all of the imported files for a circular reference and find none… So stuck. Any suggestions welcome
import os
import sys
sys.path.append(os.path.join(os.getcwd(), 'appLib', 'KB-GUI'))
sys.path.append(os.path.join(os.getcwd(), 'appLib'))
from tkinter import *
from SlotMapper import SlotMap
from ShelfTypeSelection import ShelfTypeSelector
from PackTypeSelection import PackTypeSlotMappingSelector
from EntryWidgets import EntryBase, ShelfSlotEntry
The reason this is not working is because your file is named SlotMapper.pyw. The line
from SlotMapper import SlotMap
is trying to import SlotMap from your current file, hence the error. Try renaming your file to slotmapper_test.pyw or something like that, and everything should work as expected. You don't want your code files to have the same names as any modules you're trying to import, as the import mechanism will try to find the classes/functions there first, instead of searching your modules first.

ImportError and Django driving me crazy

OK, I have the following directory structure (it's a django project):
-> project
--> app
and within the app folder, there is a scraper.py file which needs to reference a class defined within models.py
I'm trying to do the following:
import urllib2
import os
import sys
import time
import datetime
import re
import BeautifulSoup
sys.path.append('/home/userspace/Development/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
from project.app.models import ClassName
and this code just isn't working. I get an error of:
Traceback (most recent call last):
File "scraper.py", line 14, in
from project.app.models import ClassName
ImportError: No module named project.app.models
This code above used to work, but broke somewhere along the line and I'm extremely confused as to why I'm having problems. On SnowLeopard using python2.5.
import sys
sys.path.append ('/path/to/the/project')
from django.core.management import setup_environ
import settings
setup_environ(settings)
from app.models import MyModel
Whoa whoa whoa. You should never ever have to put your project name in any of your app code. You should be able to reuse app code across multiple projects with no changes. Pinax does this really well and I highly recommend checking it out for a lot of django best practices.
The worst thing you could do here is to hard code your absolute path into your app or settings. You shouldn't do this because it will break during deployment unless you do some import local_settings hacking.
If you have to access the project root directory, try what pinax has in settings.py...
import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
The thing is that it looks like you are trying to access the models module within the same app and this is waaay easier.
To import models.py inside scraper.py in the same directory just use import models or import models as app_models if you already have something named models in scraper.py (django.db.models for instance). Are you familiar with Python module conventions?
However, the best way is probably to stick with the django idiom, from ... import ... statement:
from app import models
If this doesn't work automatically, then something is wrong in your settings.py.
You don't indicate if project is located in /home/userspace/Development/. I'll assume that it is.
Make sure there's an (empty by default) file named __init__.py in project and another one in app.
EDIT: Next thing to try: Fire up the Python command line in the script's directory and try the following:
import project
import project.app as app
import project.app.models as models
models.__dict__.keys()
Do they all work? If so, what is the last line's output? If not, which dies first?

Categories