import my module

This is the place for queries that don't fit in any of the other categories.

import my module

Postby duxbuz » Sun Jan 12, 2014 9:08 am

I have been trying to import a module of mine, actually its not really much of a module its just a little program with 2 functions in.

I use :
Code: Select all
import sys
sys.path.append("c:\users\me\documents\pythondev")

or
Code: Select all
import sys
sys.path.append("")

(as I have my module in my current working directory)


But when I import it via :

Code: Select all
import myMod

it just seems to run then entire code.( I also sometimes get auto completion listing the functions within idle with myMod. but this seems intermittent.)

Another thing I tried was just importing the functions with
Code: Select all
from myMod import func1


Basically what I want to be able to do is have access to functions in another program by importing it. Is this possible? Does this only work with code written in a format that changes them from a program to an actual module. So it would not work to just import a previously written program that happens to have functions in it?

appreciate for any help with this

Regards
duxbuz
 
Posts: 9
Joined: Sat Jan 11, 2014 8:37 am

Re: import my module

Postby snippsat » Sun Jan 12, 2014 10:33 am

it just seems to run then entire code.( I also sometimes get auto completion listing the functions within idle with myMod. but this seems intermittent.)

Code: Select all
#bar.py
def foo(arg):
    result = arg ** arg
    return result

print(foo(5))

Import this code.
Code: Select all
>>> import bar
3125

Hmm not what i want code is run when import,this is where if __name__ == '__main__': come to rescue.
Version-2.
Code: Select all
#bar.py
def foo(arg):
    result = arg ** arg
    return result

if __name__ == '__main__':
    print(foo(5))

Import this code.
Code: Select all
>>> import bar
>>> #Nothing happends as wanted
>>> bar.foo(8)
16777216

Last use import as this,then you can call foo() without using bar name.
Code: Select all
>>> from bar import foo
>>> foo(25)
88817841970012523233890533447265625
User avatar
snippsat
 
Posts: 266
Joined: Thu Feb 21, 2013 12:04 am

Re: import my module

Postby duxbuz » Sun Jan 12, 2014 1:09 pm

Great thanks
duxbuz
 
Posts: 9
Joined: Sat Jan 11, 2014 8:37 am

Re: import my module

Postby duxbuz » Sun Jan 12, 2014 1:34 pm

hi

I had another issue related to this question.

It seems that the program importing myModule possibly doesn't import the myModule defined imports. Is this a possibility?

its just my myModule has some function that requires
Code: Select all
colormode(255)


and this doesn't seem to get called.... oh its ok I realised that the colormode(255) is outside my functions.

As an afterthought rather than declaring this colormode(255) in both my functions is there a better way to do it? Should I be using a class maybe? So I can share my program elements that are shared with each function such as this colormode(255) example

Thanks
duxbuz
 
Posts: 9
Joined: Sat Jan 11, 2014 8:37 am

Re: import my module

Postby duxbuz » Sun Jan 12, 2014 1:43 pm

actually this didn't work for me!

I put colormode(255) inside my functions but it seems to not have got called

The error:
TurtleGraphicsError: bad color sequence: (201, 9, 253)

only seems avoidable by having the declaration that changes the colormode
colormode(255)

But I am missing something, possible the preceding namespace? for colormode(255), if that's the correct terminology(namespace) the ********.colormode(255)

I don't know but I will figure it out
duxbuz
 
Posts: 9
Joined: Sat Jan 11, 2014 8:37 am

Re: import my module

Postby stranac » Sun Jan 12, 2014 2:35 pm

Showing some code would make understanding your problem easier...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: import my module

Postby BapeNAsians » Mon Jan 13, 2014 12:04 pm

Can someone explain how
Code: Select all
sys.path
is a list and a module or class holding methods functions?
BapeNAsians
 
Posts: 44
Joined: Fri Jan 03, 2014 4:25 am

Re: import my module

Postby snippsat » Mon Jan 13, 2014 12:40 pm

Documentation for sys.path
sys.path is the sequence of folders where python finds importable modules.

So if i place my bar.py module in a folder that is not in sys.path Python will not find it and give an error back.
Code: Select all
>>> import bar
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ImportError: No module named bar

If i place it a folder where sys.path look it will work fine.
Code: Select all
>>> import bar
>>> bar.foo(3)
27

You can append or make permanent change to sys.path for folder you like to use.
User avatar
snippsat
 
Posts: 266
Joined: Thu Feb 21, 2013 12:04 am


Return to General Coding Help

Who is online

Users browsing this forum: Majestic-12 [Bot] and 6 guests