Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95215

Re: Importing is partially working...

References <D1ED09E0.10CC86%Dwight@GoldWinde.com>
Date 2015-08-10 20:11 +1000
Subject Re: Importing is partially working...
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.36.1439201482.3627.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Aug 9, 2015 at 3:45 PM, Dwight GoldWinde <Dwight@goldwinde.com> wrote:
> name = 'Jim'
> coach = 'Dwight'
> import importlib
> sentence = 'Hi, there, ' + name + '. My name is ' + coach + '. I will be
> your coach today.'
> from Functions.py import humprint
> humprint (sentence)
>
> Traceback (most recent call last):
>
>   File "Intro.py", line 5, in <module>
>
>     from Functions.py import humprint
>
> ImportError: No module named 'Functions.py'; 'Functions' is not a package
>
>
>
> So, it seems like it is accessing the module, but not the function?

You're almost there! But in Python, you don't import something from a
specific file - you import from a module, and the Python interpreter
is free to locate that file anywhere that it can. It might be
implemented in C, and be stored in Functions.so (on Unix-like systems)
or Functions.dll (on Windows); it might be precompiled and loaded from
Functions.pyc; it might come from a zip file, or some other form of
special import source. So all you say is:

from Functions import humprint

and Python does the rest. Yep, that's all the change you need, and
your code will most likely work. (I haven't tested it, but it'll
probably work.)

Have fun!

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Importing is partially working... Chris Angelico <rosuav@gmail.com> - 2015-08-10 20:11 +1000

csiph-web