Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3 Module Import Help
#11
Just to confirm, the second code is the one you're importing, correct? I thought it was the main() call at first too, but I understood that to be your program, not the imported module.
Reply
#12
did you properly indent main?
Are there other places where you automatically execute code in module 1?
each of these will have to be qualified with similar code to prevent the module from executing.
If you don't have any hard coded code execution statements, nothing should execute upon import

i thought the first script was being imported into the second, but I see now that it's the other way around, and I just noticed that the second script was 'C'. That's a different ball game.
Reply
#13
Yes, the second script is the one i wish to import, the one with the first command as:

from getpass import getpass

yes, i believe so, how do i properly insert a message to my post? I don't want to do it incorrectly but not sure from the post you linked earlier how to send one to see how i indented it correctly.
Reply
#14
The problem with the second one is that it is just a script. On import, it will execute once and be done. To stop this, you need to organize the code into functions that can be called in your main code. Any code that is not part of a function body is executed on import.
Reply
#15
So in order to have it all imported and executed at once when requested i would have to include all of the contents of the external file into one big function?
Reply
#16
most of the code in the second script is not within function definition and by nature of that will execute immediately on loading code.
You will have to place this code in functions so that this doesn't happen.

you can fix this by placing code in functions like:

def startup():
    # This ask the user to enter their SSH username to create a variable.
    # It then requests the user to enter their SSH password to create a second variable.

    username = input('Please enter your SSH username: ')
    password = getpass()

    # This opens the file 'routercommands_file' for use in the script.

    with open('routercommands_file') as f:
        commands_list = f.read().splitlines()

        # This opens the file 'routerdevices_file' for use in the script.

    with open('routerdevices_file') as f:
        devices_list = f.read().splitlines()
and again at end:
if __name__ == '__main__':
    startup()
and I see not 'C', the dictionary threw me off for a second
Reply
#17
okay so after creating it as a function in the form you have said i simply call that function in the location i want it to run within my main script and it should work.

I'm assuming 'startup():' is a custom name that you can give the function at your own desire?

would i have to type:
Larz60+
if _name_ == '_main_' :
startup()
[/python]

for each external file i wanted to import or just add a list of file names indented under:

if _name_ == '_main_' :
Reply
#18
The line

if __name__ == '__main__' :
is used to prevent code from running unless you are using that module as the main module. It is often used for short tests of a module. It is not required.
Reply
#19
So, setting it as a function should solve the problem I am having without having to add in the bit about:

if _name_ == '_main_' :

After the script has been defined as a function what command would be entered into the main script to call that specific function when needed?

sorry for all the questions, im quite new to python and modules overall.

By creating the second script into a function it has now stopped it from automatically starting, so thank you both very much for that, im just hoping to find out how to call it now if either of you are able to help? :)
Reply
#20
you must indent code under:
if __name__ == ''__main__':

Otherwise it will have no effect.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 435 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,439 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 724 Aug-06-2023, 01:09 AM
Last Post: aupres
  import module error tantony 5 3,461 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Import a module one step back of the path prathampatel9 1 1,083 Sep-21-2022, 01:34 PM
Last Post: snippsat
  Import a module for use in type hint? Milosz 0 1,492 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 2,821 Oct-27-2021, 03:32 AM
Last Post: Tek
  import module with syntax error Skaperen 7 5,324 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  'urllib3' Module not found when import 'requests' spanz 5 10,306 Jan-06-2021, 05:57 PM
Last Post: snippsat
  Problem with Flask Bcrypt import module marcello86 2 5,746 Aug-31-2020, 08:10 PM
Last Post: marcello86

Forum Jump:

User Panel Messages

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