7 Replies - 985 Views - Last Post: 02 September 2012 - 06:26 AM Rate Topic: -----

#1 Sascha_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-September 12

Inlcude a .py-File into another .py-File

Posted 01 September 2012 - 04:00 AM

Hi Folx.

I have a little Problem. I have two Python-Files.

bot.py (for execute)
ascii.py (for data-storage)

The content of ascii.py should be parsed into the bot.py. It should look like the Content of ascii-py is written directly into the bot.py if the bot.py is executed. Tried several Things but no success.

In PHP for example you can include a file "easyli". I need the Pendant for Python. Any Ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Inlcude a .py-File into another .py-File

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Inlcude a .py-File into another .py-File

Posted 01 September 2012 - 04:02 AM

View PostSascha_77, on 01 September 2012 - 01:00 PM, said:

The content of ascii.py should be parsed into the bot.py.


Why? In what way does simply importing the ascii module not suffice?
Was This Post Helpful? 0
  • +
  • -

#3 Sascha_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-September 12

Re: Inlcude a .py-File into another .py-File

Posted 01 September 2012 - 04:05 AM

Ok ... here is an example:

bot.py:

Function 1
Function 2
Function 3


ascii.py:

Import 1
Import 2
Import 3

If the bot.py is executed it should look like this for the script:

Function 1
Function 2
Function 3
Import 1
Import 2
Import 3
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Inlcude a .py-File into another .py-File

Posted 01 September 2012 - 04:11 AM

I do not understand that example. Are "Function i" in your bot.py function definitions? Or function calls? And what are the "Import i" supposed to represent? Import statements?

If you're trying to say that ascii.py contains a bunch of import statements and you want those imported modules to also be available in bot.py (without having to repeat the same import statements in bot.py of course), then that will work fine. Importing a module also imports all modules imported into that module.

This post has been edited by sepp2k: 01 September 2012 - 04:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 Sascha_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-September 12

Re: Inlcude a .py-File into another .py-File

Posted 01 September 2012 - 04:23 AM

Ok let me explain with another example:


bot.py

Text 1
Text 4
Text 5

ascii.py

Text 2
Text 3

Should be merged to:
Text 1
Text 4
Text 5
Text 2
Text 3
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Inlcude a .py-File into another .py-File

Posted 01 September 2012 - 04:32 AM

Your examples don't really tell me why using import won't work for you.
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Inlcude a .py-File into another .py-File

Posted 02 September 2012 - 04:14 AM

Those examples don't even make sense, really. Could you be more specific in what you're trying to do and what isn't working?

EDIT:
It sounds like he wants the code in the module to execute by virtue of importing it rather than explicitly calling functions within it...

This post has been edited by atraub: 02 September 2012 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

#8 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 697
  • Joined: 19-January 09

Re: Inlcude a .py-File into another .py-File

Posted 02 September 2012 - 06:26 AM

I am not sure what you mean with your examples, but from what I can gather, you want to run bot.py and use the functions of ascii.py in it? If that is right, then importing it should work fine (as sepp2k mentioned), by doing something like this:
#this is the bot.py
import ascii

#~Code in bot.py ~
print text1
print ascii.process(text2)
print text3
#etc.


and in your ascii, you should have something like

def process(text):
    text += '-Extra'
    return text




That should work? The include that you are talking about in PHP, doesn't really need to be used in python because of the way the module systems work. When you run your code and you make a call to an imported function, it is like the code is where you made the call, but really it is just in the memory with the other code. I think you may have been misunderstanding how this worked.
Otherwise, it may just be easier to put the code into the bot.py file as a function or where you need it?

I hope this is what you wanted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1