0

I want to make my own programming language based on python which will provide additional features that python wasn't provide, for example to make multiline anonymous function with custom syntax. I want my programming language is so simple to be used, just import my script, then I read the script file which is imported my script, then process it's code and stop anymore execution of the script which called my script to prevent error on syntax...

Let say there are 2 py file, main.py and MyLanguage.py
The main.py imported MyLanguage.py
Then how to get the main.py file from MyLanguage.py if main.py can be another name(Dynamic Name)?

Additional information: I using python 3.4.4 on Windows 7

Hzzkygcs
  • 1,532
  • 2
  • 16
  • 24
  • I think you're confusing two things: naming a python module and naming a method (or whatever) defined in that module. Besides programming language design is much more complicated than what you're thinking it is. – Colonder Dec 30 '17 at 14:47
  • However you can check out https://stackoverflow.com/questions/301134/dynamic-module-import-in-python – Colonder Dec 30 '17 at 14:49

1 Answers1

3

Like Colonder, I believe the project you have in mind is far more difficult than you imagine.

But, to get you started, here is how to get the main.py file from inside MyLanguage.py. If your importing module looks like this

# main.py
import MyLanguage

if __name__ == "__main__":
    print("Hello world from main.py")

and the module it is importing looks like this, in Python 3:

#MyLanguage.py
import inspect

def caller_discoverer():
    print('Importing file is', inspect.stack()[-1].filename)

caller_discoverer()

or (edit) like this, in Python 2:

#MyLanguage.py
import inspect

def caller_discoverer():
    print 'Importing file is', inspect.stack()[-1][1]

caller_discoverer()

then the output you will get when you run main.py is

Importing file is E:/..blahblahblah../StackOverflow-3.6/48034902/main.py
Hello world from main.py

I believe this answers the question you asked, though I don't think it goes very far towards achieving what you want. The reason for my scepticism is simple: the import statement expects a file containing valid Python, and if you want to import a file with your own non-Python syntax, then you are going to have to do some very clever stuff with import hooks. Without that, your program will simply fail at the import statement with a syntax error.

Best of luck.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • I tried to run the code you have given above and I got this error:`AttributeError: 'tuple' object has no attribute 'filename'` in "MyLanguage.py", line 5, in caller_discoverer – Hzzkygcs Jan 09 '18 at 11:55
  • If i add this line to caller_discoverer `global a a=inspect.stack()` and add a code in main.py `for i in MyLanguage.a: print(i[1])` then I got `D:/Projects/Python/Test\MyLanguage.py D:/Test\MyLanguage.py \n \n \n \n \n \n \n D:/Test/main.py \n C:\Python34\lib\idlelib\run.py \n C:\Python34\lib\idlelib\run.py \n ` which means the main.py path is not located at [-1] – Hzzkygcs Jan 09 '18 at 12:09
  • Your question is tagged Python-3.x and I know you believe you are running Python 3, but the error you report: `AttributeError: 'tuple' object has no attribute 'filename'` is precisely what I get if I run the code in the answer under Python 2.7.13. I've edited the answer to show you what you need to do in Python 2. For the Python 2 version I deliberately changed `print` in `MyLanguage.py` from something that might be a function call, to a statement. If you are running Python 3 you will get a syntax error at that point. If you are running Python 2, you won't. – BoarGules Jan 09 '18 at 21:15