Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3697452
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:51:42+00:00 2026-05-19T04:51:42+00:00

Note: Solved. It turned out that I was importing a previous version of the

  • 0

Note: Solved. It turned out that I was importing a previous version of the same module.

It is easy to find similar topics on StackOverflow, where someone ran into a NameError. But most of the questions deal with specific modules and the solution is often to update the module.

In my case, I am trying to import a function from a module that I wrote myself. The module is named InfraPy, and it is definitely on sys.path. One particular function (called listToText) in InfraPy returns a NameError, but only when I try to import it into another script. Inside InfraPy, under if __name__=='__main__':, the listToText function works just fine. From InfraPy I can import other functions with no problems. Including from InfraPy import * in my script does not return any errors until I try to use the listToText function.

How can this occur?
How can importing one particular function return a NameError, while importing all the other functions in the same module works fine?

Using python 2.6 on MacOSX 10.6, also encountered the same error running the script on Windows 7, using IronPython 2.6 for .NET 4.0

Thanks.

If there are other details you think would be helpful in solving this, I’d be happy to provide them.

As requested, here is the function definition inside of InfraPy:

def listToText(inputList, folder=None, outputName='list.txt'):
    '''
    Creates a text file from a list (with each list item on a separate line). May be placed in any given folder, but will otherwise be created in the working directory of the python interpreter.
    '''
    fname = outputName
    if folder != None:
        fname = folder+'/'+fname
    f = open(fname, 'w')
    for file in inputList:
        f.write(file+'\n')
    f.close() 

This function is defined above and outside of if __name__=='__main__':

I’ve tried moving InfraPy around in relation to the script. The most baffling situation is that when InfraPy is in the same folder as the script, and I import using from InfraPy import listToText, I receive this error: NameError: name listToText is not defined. Again, the other functions import fine, they are all defined outside of if __name__=='__main__': in InfraPy.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:51:43+00:00Added an answer on May 19, 2026 at 4:51 am

    This could happen if the module has __all__ defined

    Alternatively there could be another version of the module in your path that is getting imported instead of the one you are expecting

    Is the NameError about listToText or is it something inside the function causing the exception?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: This was posted when I was starting out C#. With 2014 knowledge, I
Note that I am not asking which to choose (MVC or MVP), but rather
NOTE: Solved my own problem, it seems. See the edits. I am trying to
Reading this question I found this as (note the quotation marks) code to solve
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
NOTE: I am not set on using VI, it is just the first thing
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
Note : The code in this question is part of deSleeper if you want
Note The question below was asked in 2008 about some code from 2003. As
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.