I have this IRC bot that I am hard coding from scratch just for kicks. In it I pasted all of my functions to a separate file so that it saves spaces on my eyes when I code the mainframe. So on my imports I have:

import socket, re, time
import urllib.request, os, sys
import pickle
from RushFunctions import *

The bottom line is my custom module with all of my functions. With in there in one of my functions I call:

url = urllib.request.Request(url)

But I get the error:

File "/home/ron/RushFunctions.py", line 6, in stats
    url = urllib.request.Request(url)
NameError: global name 'urllib' is not defined

If I paste all of my functions into my main file, it all works fine and dandy. I have found that if I import those modules from within my module it works fine, but will that effect how I use them in my main module.

The same goes for the var if I do the ugly imports to get the above to work:

SkillsArray = stuffhere

I try and use it in one of my custom functions and I get that the variable is undefinded.

Recommended Answers

All 4 Replies

Well you can not just call your module and expect the imported modules to work right away.

Say we got text.py which contains...

import socket, re, time
import urllib.request, os, sys
import pickle
from RushFunctions impor

to have any of the imported modules called from text.py, you must inherit the module into your code in text.py like...

class foo(socket):
   def __init__():

Then import the module into text2.py for action

from text import foo ## which is the socket

This is give you the idea on how to include a modules in a project like yours.

You will have to bear with me. I am still new to the whole OO way of programing. If I understood you right, I would do:

from RushFunctions import *
import socket, re, time
import urllib.request, os, sys
import pickle
class foo:
	def __init__():

I tried how you showed but I was getting the 3 arguements given, only 2 accepted.

Now when I do that it acts like it doesn't import any function from my module.

Traceback (most recent call last):
  File "rush.py", line 1, in <module>
    from testerfile import *
  File "/home/ron/testerfile.py", line 1, in <module>
    from rush import foo
  File "/home/ron/rush.py", line 48, in <module>
    Stats = stats(StatsNick)
NameError: name 'stats' is not defined

I clearly imported it though, right? Because this function is located in RushFunctions.py and I did import it.

in a class, the first argument to each member function must be self . If you count up, you'll see that is the one that is missing.

However, at a more basic level, you do not need to use classes and inheritance to deal with your issue (although you may find you want to, and that would be fine). Instead, you need to consider that an imported module is kind of one-way: When you import from a module, the main script has access to everything in that imported module, but the module itself has to be self contained. Anything it needs from main needs to be passed as a parameter (which might be a parameter to a a constructor of a class instance).

Hope that makes sense. If not, feel free to ask again.

Oh, okay. Thanks. Seemed to help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.