Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing a module
#11
(Nov-05-2016, 03:20 PM)SrirachaSauceLover Wrote: I use IDLE Python 3.5.2 running on Windows 7.
Can give demo,can use IDLE but not a fan it at all Wink 

In IDLE file --> New file paste in code under,save it as chaos.py(in Python 3.5 folder).
# File: chaos.py
# A simple program illustrating chaotic behavior.
def main():
    print("This program illustrates a chaotic function")
    inp = float(input("Enter a number between 0 and 1: "))
    for i in range(10):
        inp = 3.9 * inp * (1 -inp)
        print(inp)

if __name__ == '__main__':
    # Will now run as a script
    # But will not run when you import it(have to call main())
    main()
Close all(IDLE) then open:
File --> open chaos.py.
Run --> Run Module(F5)
Output:
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>>  This program illustrates a chaotic function Enter a number between 0 and 1: .5 0.975 0.09506250000000008 0.33549992226562525 0.8694649252590003 0.44263310911310905 0.962165255336889 0.1419727793616139 0.4750843861996143 0.9725789275369049 0.1040097132674683 >>> #Now will import it an run the same >>> import chaos >>> main() This program illustrates a chaotic function Enter a number between 0 and 1: .5 0.975 0.09506250000000008 0.33549992226562525 0.8694649252590003 0.44263310911310905 0.962165255336889 0.1419727793616139 0.4750843861996143 0.9725789275369049 0.1040097132674683 >>> 
Quote:If your using a tutorial that puts in eval() in from direct input of a user, and does not inform you of the security risks...get a new tutorial.
It's now an old book 2003 i think,and yes he use eval() in a lot places(not just in input) as he should not have done.
Had to check,has a copy of the book as pdf Snooty
Reply
#12
SrirachaSauceLover
All you do is delete the spaces in front of the text. In your code you had
main()
which has 8 spaces in front of it. Just delete them
Reply
#13
(Nov-05-2016, 08:39 PM)Barrowman Wrote: SrirachaSauceLover
All you do is delete the spaces in front of the text. In your code you had
       main()
which has 8 spaces in front of it. Just delete them

I do this and am greeted with:

SyntaxError: unindent does not match any outer indentation level
Reply
#14
(Nov-05-2016, 08:39 PM)Barrowman Wrote: All you do is delete the spaces in front of the text. In your code you had
main()
which has 8 spaces in front of it. Just delete them
Can also delete main() all together,dos not need that call when import it.
# File: chaos.py
# A simple program illustrating chaotic behavior.
def main():
   print("This program illustrates a chaotic function")
   inp = float(input("Enter a number between 0 and 1: "))
   for i in range(10):
       inp = 3.9 * inp * (1 -inp)
       print(inp)
Test import:
Output:
>>> import chaos >>> chaos.main() This program illustrates a chaotic function Enter a number between 0 and 1: .5 0.975 0.09506250000000008 0.33549992226562525 0.8694649252590003 0.44263310911310905 0.962165255336889 0.1419727793616139 0.4750843861996143 0.9725789275369049 0.1040097132674683
Look a little at at if __name__ == '__main__': and what it does.
Reply
#15
__name__ is the name of the scope in which top-level code executes.
a modules name is '__main__' when called from standard input, so
when checking for comparison of the two, is __name__ == '__main__'
will execute ifthe module is run like:
if __name__ == '__main__':
    do this
but not when imported as in:

import modulename
great for development
Reply
#16
I removed the second "main()" from my code and still receive the message:


import chaos
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import chaos
  File "C:\Program Files (x86)\Python35-32\chaos.py", line 1
    Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
             ^
SyntaxError: invalid syntax
Reply
#17
constantly giving us the traceback isnt giving us any more information to work with.

Have you tried snippsats code executing it exactly as he has done it, and how he executed it
http://python-forum.io/Thread-Importing-...94#pid4194
Recommended Tutorials:
Reply
#18
(Nov-05-2016, 09:17 PM)SrirachaSauceLover Wrote:
(Nov-05-2016, 08:39 PM)Barrowman Wrote: SrirachaSauceLover
All you do is delete the spaces in front of the text. In your code you had
       main()
which has 8 spaces in front of it. Just delete them

I do this and am greeted with:

SyntaxError: unindent does not match any outer indentation level

Okay so it seems you have some problem with making sure that everything is aligned as it should be so let's see if you can get it right.
>>>> Looking at the version I posted <<<< Just to remove any confusion.
When you create the function with def main():
it should start on a line with no spaces to the left of it.
The next line 
inp = raw_input("Enter a value between 0 and 1: ")
should have 4 spaces to the left of it.
After that x = float(inp)
and  
for i in range(10):
should each have 4 spaces in front of them.
Then 
x = 3.9 * x * (1 - x)
and
print x
should have 8 spaces in front of them
Finally 
main()
should have no spaces in front of it.
If you get all that right you will not get the error.

The interpreter needs this consistent format so it knows where the def ends and where the for loop ends.
Note that if the def is itself within another def such as 
class example(): 
 the def needs to have 4 spaces to the left of it and all the other bits need an extra 4 spaces to the left of them.
I have said 4 spaces and then 8 spaces because that is the standard way and a good habit to get into. You could use 3 and 15 or 7 and 11  and so on but whatever you use it has to be consistent or you will get the unindent error.
Often copy and paste can cause this error.
Reply
#19
(Nov-06-2016, 08:10 AM)SrirachaSauceLover Wrote:
import chaos
Traceback (most recent call last):
    Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
             ^
SyntaxError: invalid syntax

...is the first line of chaos.py "Python 3.52 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32"?
I find it very, very strange that the syntax error is pointing to something that shouldn't be anywhere in the file.
Reply
#20
I found the mistake: I made the module in the shell. I remade the module in a text editor and then again in the IDLE editor. Everything works now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 267 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  no module named 'docx' when importing docx MaartenRo 1 816 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  My code displays too much output when importing class from a module lil_e 4 1,140 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Importing module in jupyter Noteboook ajitnayak1987 0 1,733 Jun-04-2021, 12:26 PM
Last Post: ajitnayak1987
  ERROR: importing desired module mbgamer28 0 1,669 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  importing module - not working jdhamblett 3 2,991 Jun-22-2020, 07:33 PM
Last Post: jdhamblett
  importing same python library in multiple custom module escape_freedom13 6 3,782 May-10-2020, 07:01 PM
Last Post: escape_freedom13
  Importing module from a package results in import error goghvv 2 2,376 Mar-27-2020, 07:13 PM
Last Post: goghvv
  Please help: problem installing/importing langdetect module in Jupyter Notebook ledgreve 3 7,256 Dec-30-2019, 08:17 AM
Last Post: LeanbridgeTech
  Problem with importing and using collections module pythomdummy 3 5,788 Nov-14-2019, 08:48 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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