Jump to content
  • Advertisement
Sign in to follow this  
Polantaris

Multiple .py files

This topic is 4520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!