class - How do you import files into the Python shell? -


i made sample file uses class called names. has initialization function , few methods. when create instance of class retrieves instance's first name , last name. other methods greet instance , departure message. question is: how import file python shell without having run module itself?

the name of file classnames.py , location c:\users\darian\desktop\python_programs\experimenting

here code looks like:

 class names(object):      #first function called when creating instance of class      def __init__(self, first_name, last_name):          self.first_name = first_name          self.last_name = last_name        #class method greets instance of class.       def intro(self):           print "hello {} {}!".format(self.first_name, self.last_name)        def departure(self):           print "goodbye {} {}!".format(self.first_name, self.last_name) 

but error:

traceback (most recent call last):    file "<pyshell#0>", line 1, in <module>      import classnames.py  importerror: no module named classnames.py 

i not clear expect , see instead, handling of module works math , other modules:

you import them. if happens first time, file taken , executed. left in namespace after running available outside.

if code contains def, class statements , assignments, wou won't notice happens, because, well, nothing "really" happens @ time. have classes, functions , other names available use.

however, if have print statements @ top level, you'll see indeed executed.

if have file anywhere in python path (be explicitly or because in current working directory), can use like

import classnames 

and use contents such

n = classnames.names("john", "doe") 

or do

from classnames import names n = names("john", "doe") 

don't import classnames.py, try import module py.py package classnames/.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -