importing files


EXAMPLE
The django web framework is used to create the application which convert the string into uppercase using str.upper method. The string_method_upper.py calls the string_method.views.py file to render the function.
Windows CMD commands for creating virtualenv, startproject, startapp, and running server.
  • virtualenv string
  • cd string
  • .\Scripts\activate
  • pip install django
  • python .\Scripts\django-admin.py startproject str
  • cd str
  • python manage.py runserver
  • python manage.py startapp string_methods
  • python manage.py startapp string_method_upper
  • python manage.py runserver



string_method_upper/views.py


def string_method_upper (request):
    return string_upper(request)

string_methods/views.py


def string_upper(request):
    return HttpResponse("Python is a simple, powerful and high-level programming language "
                        "and the logic and structure is consistent with the way we "
                        "reasons. ".upper())


urls.py

from string_methods.views import*
from string_method_upper.views import *

urlpatterns=patterns('', url(r'^string_method_upper/', string_method_upper),)

settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles','string_methods', 'string_method_upper',
)




The file is saved as pyramid.py in the folder C:\code\pyramid.py.
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

def test(request):
    x=range(1,0)
    y=13
    if y in x:
        return Response('y is in x')
    else:
        return Response('y is not in x')

def main():
    config = Configurator()
    config.add_route('index', '/')                        
    config.add_view(test, route_name='index')
    app = config.make_wsgi_app()
    return app

if __name__ == '__main__':
    app = main()
    server = make_server('0.0.0.0', 1000, app)
    print ('Starting up server on http://localhost:1000')
    serer.serve_forever()


EXAMPLE
The file saved as main.py saved in C:\code\pyramid.py called the file pyramid.py saved in same folder.
 import pyramid

def main():
    pyramid()







The function is called from other .py file in main.file. First the file is saved as a calculate.py at C:\code\calculate.py.
def sub1(a,b):
    a=float(a)
    b=float(b)
    c=a-b
    print(c)


def add1(a,b):
    a=float(a)
    b=float(b)
    c=a+b
    print(c)
 

The file is saved as main.py at C:\code\calculate.py.
 
from calculate import sub1

x=4
y=6
sub1(x,y)

>>> 
-2.0
>>> 



importing files

  • .py
  • .html
Comments