1

In src/webprofiles/__init__.py I have

def match(string)

Now how can I make a call to this match from `src/python.py, which contains

from webprofiles import *

for x in text
    a= webprofiles.match(x)

Its giving me an error

NameError: global name 'webprofiles' is not defined

4 Answers 4

3

When you use from import form, you must call function without module prefix. just call the functions and attributes via their names.

from webprofiles import *

for x in text:
    a= match(x)

but i suggest to DO NOT use wildcard('*') imports. use this instead:

from webprofiles import match

for x in text:
    a= match(x)
Sign up to request clarification or add additional context in comments.

1 Comment

May be the name of the file is not correct, make sure you have a file named:__init__.py in the webprofiles directory
1

The syntax from x impoort * means that everything will be imported, in effect, into the global namespace. What you want is either import webprofiles followed by webprofiles.match or from webprofiles import * followed by a call to plain match

1 Comment

thanx import webprofiles followed by webprofiles.match worked but from webprofiles import * followed by a plain match gave an error!!
1

Just import webprofiles, not *:

import webprofiles

for x in text
    a = webprofiles.match(x)

Comments

0

What you have there seems to me 2 files and you want to run a file which imports the methods contained in an other file:

import /webprofiles/init


init.match(x)

after modifiying your question:

import /webprofiles/__init__


__init__.match(x)

btw when you import something:

import my_file #(file.py)

my_file.quick_sort(x)

^^^^^^ you have to call myfile as you call normally an object

from my_file import *
#that is read as from my_file import everything
#so now you can use the method quick_sort() without calling my_file
quick_sort(x)

1 Comment

Google __init__.py, it's a standard file used in packages and has nothing to do with constructors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.