2

I have two files 'mod1.py' and 'mod2.py'.

mod1 requires request module to function. But I have not imported them in the mod1 instead I have imported both request and mod1 module in mod2.

But

I get an error 'name 'requests' is not defined'. I know it works if i Import 'request' module in mod1 directly it works fine. But I have other modules I want to use that requires 'request' module. So how do I import the module once and make in accessible to all the other modules ?.

mod1.py

class getUrl():
    def __init__(self, url):
        self.url = url

    def grab_html(self):
        html = requests.get(self.url).text
        return html

mod2.py

import requests
import mod1

module1 = mod1.getUrl('https://www.wikipedia.org/')
HTML = module1.grab_html()

Edit: Complete error

Traceback (most recent call last):
  File "C:\Users\camel\Desktop\test\mod2.py", line 5, in <module>
    HTML = module1.grab_html()
  File "C:\Users\camel\Desktop\test\mod1.py", line 6, in grab_html
    html = requests.get(self.url).text
NameError: name 'requests' is not defined
[Finished in 0.5s with exit code 1]
[shell_cmd: python -u "C:\Users\guru\Desktop\test\mod2.py"]
hyperTrashPanda
  • 838
  • 5
  • 18
CoDINGinDARK
  • 244
  • 4
  • 16
  • at what line does the error come from? could you please post the full error? – Josh Duzan Feb 07 '19 at 13:16
  • 2
    You have to `import requests` everytime you need it. – rene-d Feb 07 '19 at 13:17
  • does it add up memory. If I keep doing it ? – CoDINGinDARK Feb 07 '19 at 13:18
  • No it shouldn't be a problem, but it's could be a bad practice, as you'll have to manually maintain imports in different files. – hyperTrashPanda Feb 07 '19 at 13:21
  • No. And no, it's not a bad practice. If a module needs a library, it has to import it. You can use [flake8](https://pypi.org/project/flake8/) to track useless import statements. – rene-d Feb 07 '19 at 13:27
  • Imported modules are cached in `sys.modules` and importing them more than once is the same run of the interpreter only loads them once—so it's relatively inexpensive to `import` them many times. – martineau Feb 07 '19 at 14:45

4 Answers4

2

When you import something, it becomes a named thing in the module that imported it. Requests is not being used driectly by mod2.py, but is by mod1.py so that's where you should import it.

You can do this, for example.

mod1.py

import requests

class getUrl():
def __init__(self, url):
    self.url = url

def grab_html(self):
    html = requests.get(self.url).text
    return html

mod2.py

import mod1

module1 = mod1.getUrl('https://www.wikipedia.org/')
HTML = module1.grab_html()

# And also access requests via mod1
indirectly = mod1.requests.get('https://www.wikipedia.org/').text
Simon Hibbs
  • 5,941
  • 5
  • 26
  • 32
0

import requests, should be in mod1.py, because it is used in the methods of the class defined in mod1.py. you could import it in both places if it was needed in mod2.py as well.

Josh Duzan
  • 80
  • 1
  • 8
  • does it add up memory. That is what I am concerned – CoDINGinDARK Feb 07 '19 at 13:21
  • https://stackoverflow.com/questions/7372966/python-modules-when-one-imports-them-do-they-go-into-memory check out this question. Python is not really a language to concern yourself with memory. – Josh Duzan Feb 07 '19 at 13:22
0

You need to create an __init__.py file (it can be empty) file so that the folder containing mod1 is recognized as a module.

Then you can do from mod1 import *, or from path.to.mod1 import * and it will carry over all the imports over to mod2. Check out this relative answer. In my opinion this is a sensible way of doing things, as you can keep all your dependencies in a centralized location.

As you're concerned about memory utilization, take a look at another conversation on the same matter.

hyperTrashPanda
  • 838
  • 5
  • 18
0

As you are not using requests in mod2.py, you could just do import requests in mod1.py

If you are worried about the memory, it will take the same amount as you are going to use it in just one script. But if you are using if you are planning to use it in mod2.py as well, then you have to include there also.