CoDINGinDARK
CoDINGinDARK

Reputation: 346

How to make a module accessible by other modules

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"]

Upvotes: 2

Views: 1243

Answers (4)

hyperTrashPanda
hyperTrashPanda

Reputation: 868

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.

Upvotes: 0

Simon Hibbs
Simon Hibbs

Reputation: 6211

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

Upvotes: 2

Muhammed Ajwahir
Muhammed Ajwahir

Reputation: 45

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.

Upvotes: 0

Josh Duzan
Josh Duzan

Reputation: 80

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.

Upvotes: 0

Related Questions