Sign in to follow this  
Polantaris

Multiple .py files

Recommended Posts

As any one who read my original post in the Lounge knows, I'm used to the BYOND coding language. In that language, I can use multiple code files to organize my thoughts, ideas, etc. For example, my combat code for a game is in combat.dm (dm = code files), and my stat panel stuff is in statpanel.dm. Is it possible to do something of this sort in Python coding? I would assume it's just include filename.py, but I want to be sure because I'm not up to that part yet.

Share this post


Link to post
Share on other sites
All of your Python files must eventually make their way into the main file that is run by the interpreter. For example, if the first file you needed to run was py1.py, but you also needed to access data and functions in py2.py and py3.py, you'd put the following at the top of py1.py:

import py2.py
import py3.py

I'm not sure where Python searches for import modules, but if they are in the Python installation folder, they will be found. Note that in this manner, you will need to prefix all of that data in, say, py2.py with py2.*
You can get around this with the following:

from py2.py import *

This will import everything from py2.py directly into your current file. So, instead of saying py2.function_call(), you could just type function_call()

Share this post


Link to post
Share on other sites
In Python, in the most simple importation model (you can get pretty arbitrarily complex if you know what you're doing) each module is contained in a single .py file. For example, create a file called foo.py and then "import foo" within any source file that must use components of the foo module. To subsequently reference a component of the foo module, you precede it by 'foo.'

A more well constructed example:



# in foo.py

def bar(x):
return x+1

#----------------

# in main.py

import foo
print foo.bar(0) # accessing the bar object in the foo module

Share this post


Link to post
Share on other sites
Would it be okay to import every .py file I have to each other .py file? Because I switch between them all the time. Like using my BYOND example, Battle code is called in another file, but the battle code has some stuff that goes to other files, etc. So to be safe as far as I know, it would be okay to go and import every file to every other file, no? It's not like it would hurt the program at all, would it?

Edit: It seems that I don't add the .py to files in the same directory, or if not that, then at all. Because trying to put .py on the end of imports would give me a syntax, but when I take them away it worked.

[Edited by - Polantaris on June 7, 2006 7:35:53 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Polantaris
Would it be okay to import every .py file I have to each other .py file? Because I switch between them all the time. Like using my BYOND example, Battle code is called in another file, but the battle code has some stuff that goes to other files, etc. So to be safe as far as I know, it would be okay to go and import every file to every other file, no? It's not like it would hurt the program at all, would it?


You could, but unless you need the stuff contained in the modules you're importing there's no reason to.

Quote:
Edit: It seems that I don't add the .py to files in the same directory, or if not that, then at all. Because trying to put .py on the end of imports would give me a syntax, but when I take them away it worked.


You're not supposed to say "import whatever.py", it's fine, it's just "import whatever".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this