cminghao
cminghao

Reputation: 1

unable to import module that is in the same file

I am having trouble importing the testRequest.py module. However, the APIsetting.py module is in the exact same place as load.py (this script) and testRequest.py. Weirdly enough APIsetting.py will not import properly without from windows import APIsetting either.

As I run the script from a program, it gives very different problems than when run using the command prompt. The command prompt encounters problems in other parts of the code that runs fine in the program, and the program runs as per normal if import testRequest does not exist in any form.

How do I go about importing testRequest.py?

I have tried from windows import APIsetting, testRequest, import testRequest, and from windows import testRequest.

import os
import sys
import functools
from windows import APIsettings
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon, QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import *
from PyQt5 import uic
import testRequest
from classes.app import get_app
import requests
import xml.etree.ElementTree as ET
from xml.dom import minidom
from .windows import testRequest
try:
    import cPickle as pickle
except ImportError:  
    import pickle


class load_request(QDialog):


    def __init__(self):

        QDialog.__init__(self)

        get_app().window.actionPlay_trigger(None, force="pause")

        self.list = QListWidget(self)
        self.list.resize(600, 400)
        for i in range(10):
            self.list.addItem('item = %s' %(i+1))

I expect import testRequest to run smoothly, which would then allow me to access the variables inside and use them for a list. However, import testRequest is not allowing the program to run as intended.

After abit of testing I have come to the conclusion that the reason testRequest is unable to import is due to the codes in testRequest, however I can not tell which part of it actually causing the issue.

import os
import sys
import requests
import xml.etree.ElementTree as ET
from xml.dom import minidom
import APIsettings

try:
    import cPickle as pickle
except ImportError: 
    import pickle

class test_Request():
    def sent_request():
        request =""" 

            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:website="http://www.website.eu/websiteWSApi">
            <soapenv:Header/>
            <soapenv:Body>
                <website:GetList>
                    <!--Optional:-->
                    <website:_user>luca</website:_user>
                    <!--Optional:-->
                    <website:_pwd>123</website:_pwd>
                </website:GetList>
            </soapenv:Body>
            </soapenv:Envelope>"""

        headers = {"Host": "172.65.54.68","Content-Type": "text/xml","Content-Length": "length"}

        response = requests.post(url="http://172.65.54.68/websiteAPI.asmx?",headers = headers,data = request)
        return response
    def write_response(response):
        with open("testSave.xml", "r+") as f:
            f.write(response.text)

        with open("testSave.xml", "r+") as f:
            content= f.read()
            f.seek(0)
            f.truncate()
            f.write(content.replace('&lt;','<'))

        with open("testSave.xml", "r+") as f:
            content= f.read()
            f.seek(0)
            f.truncate()
            f.write(content.replace('&gt;','>'))

    def read_response():
        with open('testSave.xml', 'r') as f2:
            data= f2.read()
            start_idx = data.index('<Function')
            end_idx = data.index('</Function>')
            data = data[start_idx:end_idx + 11]
            root = ET.fromstring(data)

            #for a in root.getiterator(APIsettings.item):
                #print(a.attrib)
                #print('test1')
            with open('responseDict.pickle', 'wb') as f:

                for a in root.getiterator(APIsettings.item):
                    pickle.dump(a.attrib, f ,protocol=pickle.HIGHEST_PROTOCOL)
                    #print(a.attrib)
                    #print('test2')
            return root
    def get_variables(root):        
        with open('responseDict.pickle', 'rb') as f:
            for b in root.getiterator(APIsettings.item):
                variTest= pickle.load(f)
                print(variTest)
                print('test3')

    def main():
        sent_request()
        response_request= sent_request()
        write_response(response_request)
        read_response()
        root_response= read_response()
        get_variables(root_response)

    main()

These are the codes that exist within testRequest

Upvotes: 0

Views: 124

Answers (1)

Ramy M. Mousa
Ramy M. Mousa

Reputation: 5933

If they reside in the same directory then just use import .file_name in your case this would be:

import .testRequest

normally when you don't add . python goes to search for a complete module to navigate to.

Upvotes: 1

Related Questions