  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # python 3: import module  » python » python 3: import module

By : JTCW33
Date : October 18 2020, 03:08 PM
I hope this helps you . Notes:
understand difference between module and function. You can't import a function, in this case inputRect and calcArea since you want to create function for each process, try to utilize return in your function to get data that you need still in the spirit using function, you can separate some calculations. For example, instead of compute two rectangles in one function, create a function that only calculate an area, given width and length code :
``````def get_rect_input():
width1 = int(input("Enter the width of the first rectangle: "))
length1 = int(input("Enter the length of the first rectangle: "))
width2 = int(input("Enter the width of the second rectangle: "))
lenght2 = int(input("Enter the length of the second rectangle: "))
return width1, length1, width2, lenght2

def calculate_area(width, length):
return width * length

def show_comparation(width1, length1, width2, lenght2):
area1 = calculate_area(width1, lenght2)
area2 = calculate_area(width2, lenght2)

if area1 > area2:
print ("Rectangle number 1 is bigger than rectangle 2")
elif area1 < area2:
print ("Rectangle number 2 is bigger than rectangle 1")
else:
print ("Both rectangles have the same area")

if __name__ == "__main__":
width1, lenght1, width2, lenght2 = get_rect_input()
show_comparation(width1, lenght1, width2, lenght2)
`````` ## Can't import python module from a subdirectory with a simple import statement

By : กลับไม่ได้ ไปไม่ถึง
Date : March 29 2020, 07:55 AM
it fixes the issue It does import the module, it just doesn't make its name directly accessible. When you do import foo.bar, the name that is imported is foo, and bar is only accessible as an attribute of that. You use that form of import if that is what you want; that's what it's for. If you don't want that, use a different form of the import statement.
If you want to be able to type module instead of modules.module, either do import modules.module as module, as you found, or do from modules import module. ## Python circular import, `from lib import module` vs `import lib.module`

By : user7591529
Date : March 29 2020, 07:55 AM
should help you out Since there did not seem to be a direct way to address the circular import, I went with a workaround.
In my actual use case, module a imported module b only to call the function b.fn, so I decided to put fn in a third module c and import c instead:
code :
``````def fn():
...
``````
``````from lib import a
from lib import c
...
# Explicitly assign `fn` into this module.
fn = c.fn
``````
``````from lib import c
...
`````` ## Problems in Python with difference between import module and from module import

By : Watcha Ra
Date : March 29 2020, 07:55 AM
I wish did fix the issue.
In this case the global variable iGlobalA is imported as a copy from ModuleA
code :
``````# ModuleA
iGlobalA = []

def fA():
iGlobalA.append(1)
print( "MA: iGlobalA=", iGlobalA )

print( "Module A Initialised, iGlobalA=", iGlobalA )
`````` ## Can I import a module that has in it the python file where I wrote the import statement?

By : Naveen Sharma
Date : March 29 2020, 07:55 AM
seems to work fine I think what you are trying to do is to import FirstFolder and SecondFolder modules into main.
Now, trying to do from TopFolder import * will result in a circular import, because main will be importing itself (main.py is part of *), which will do the import * again, ... and so on. Furthermore, doing import * is discouraged (see Why is “import *” bad? ).
code :
``````TopFolder
|- FirstFolder
|- __init__.py
|- foo1.py
|- SecondFolder
|- __init__.py
|- foo2.py
|- main.py
``````
``````def bar1():
return "bar 1"
``````
``````def bar2():
return "bar 2"
``````
``````# from TopFolder import *  # Don't do this
from FirstFolder import foo1
from SecondFolder import foo2

print(foo1.bar1())
print(foo2.bar2())
``````
``````\$ python3 main.py
bar 1
bar 2
`````` ## import python dependencies in databricks (unable to import module)

By : Kent Keller
Date : March 29 2020, 07:55 AM 