Python Forum
When I import a Module it wont run
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When I import a Module it wont run
#8
(Sep-23-2019, 01:06 AM)ichabod801 Wrote: First of all, that is not the correct code or you would be getting an indentation error. Second, the if __name__ goes at the end of the file, and is only for code that should not be run when the file is imported. If you stick it in the middle of a function, the function won't do anything when it is imported.

Sorry about the late reply but Ive been a way for the last 2 weeks.
When I pasted the previous code it didnt display the way I wanted. The actual code I used is below:
import os
import globalVariables as gv 
 
def listCWD():
         
    if __name__ == '__main__':  
         
        try:
            gv.directoryList = os.listdir(gv.directoryIn)
            print('directoryList =\n',gv.directoryList)
 
        except FileNotFoundError:
            print('FileNotFoundError:\na List for the Directory gv.directoryIn cannnot be created\nbecause the Global Variable gv.directoryIn is an empty string.\n'
                  'To correct this either:\na)run the returnCWD function in this Module to assign a directory to gv.directoryIn or\nb)open the globalVariables.py file and setgv.directoryIn to = None')
 
        except TypeError:
            print('TypeError:\na List for the Directory gv.directoryIn cannnot be created\nbecause the Global Variable gv.directoryIn has been assigned to equal either:\n'
            'a)List\nb)Set\nc)Tuple\n'
            'To correct this either:\na)run the returnCWD function in this M odule to assign a directory to gv.directoryIn or\nb)open the globalVariables.py file and setgv.directoryIn to = None')
 
        except NameError:
            print('NameError:\nbecause of the Syntax Error on import a NameError now occurs in the functions code\n'
            'To correct the SyntaxError above open the globalVariables.py file and do either of the solutions below\n'
            'a)make directoryIn = an empty string\nb)make directoryIn = None')
 
listCWD()
You say that the if main code should go at the end of the file but I dont have anything to put after the if main piece of code. The file listCWD.py only contains a listCWD function and if I dont use If main the function runs on Import and I do not want it to run on import.
Reply


Messages In This Thread
When I import a Module it wont run - by PyNovice - Sep-22-2019, 06:30 PM
RE: When I import a Module it wont run - by PyNovice - Oct-14-2019, 12:06 PM
RE: When I import a Module it wont run - by buran - Oct-14-2019, 12:31 PM
RE: When I import a Module it wont run - by buran - Oct-14-2019, 03:28 PM
RE: When I import a Module it wont run - by buran - Oct-14-2019, 04:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 479 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  Why wont this path work one way, but will the other way? cubangt 2 704 Sep-01-2023, 04:14 PM
Last Post: cubangt
  problem in import module from other folder akbarza 5 1,590 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 755 Aug-06-2023, 01:09 AM
Last Post: aupres
  import module error tantony 5 3,581 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Import a module one step back of the path prathampatel9 1 1,112 Sep-21-2022, 01:34 PM
Last Post: snippsat
  Import a module for use in type hint? Milosz 0 1,519 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 2,877 Oct-27-2021, 03:32 AM
Last Post: Tek
  import module with syntax error Skaperen 7 5,427 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  'urllib3' Module not found when import 'requests' spanz 5 10,514 Jan-06-2021, 05:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020