![]() |
Simple Question - ' defined as "a". ?' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Simple Question - ' defined as "a". ?' (/thread-40059.html) Pages:
1
2
|
Simple Question - ' defined as "a". ?' - Ryan012 - May-25-2023 I have one error for no defined as "a". How can we define "a"? ![]() ![]() import a as l def sum (x, y): return x + y # calls the sum function defined in the module a print (l.sum(2, 6)) RE: Simple Question - ' defined as "a". ?' - rob101 - May-25-2023 Assuming both files are in the same directory: Save this as a.py print("Hi. My name is 'a'")Save this as the script that is doing the import import aHave a play with that and see if you can work it out. RE: Simple Question - ' defined as "a". ?' - Gribouillis - May-25-2023 (May-25-2023, 01:37 PM)Ryan012 Wrote: import a as lThe module «a» can be imported only if the file a.py is in a directory on the Python path. RE: Simple Question - ' defined as "a". ?' - Ryan012 - May-25-2023 Hi rob101 Thank you so much for your kind response and explaination. I'm very beginner so..I have no idea what I have to do.. 1) You mean I need to make a file - (1) a.py in the same directory,Right? 2) How can I Save this as the script that is doing the import ? 1 import a 3) I tried to run again but they showed me another error as the following: AttributeError : module 'a' has no attribute 'sum'. Thanks Ryan. RE: Simple Question - ' defined as "a". ?' - Ryan012 - May-25-2023 Python path means my main fils should be in the same directory. right? RE: Simple Question - ' defined as "a". ?' - Gribouillis - May-25-2023 (May-25-2023, 03:52 PM)Ryan012 Wrote: Python path means my main fils should be in the same directory. right?No, the Python module path is a list of directory that you can print like this >>> import sys >>> print(sys.path)Then you can learn how to modify this list if needed. Also don't hesitate to read the official documentation about modules, although it is perhaps too exhaustive for a first approach. RE: Simple Question - ' defined as "a". ?' - rob101 - May-25-2023 (May-25-2023, 03:51 PM)Ryan012 Wrote: Hi rob101 You're welcome. I'm going to side-line at this point, for no other reason than: if two people are trying to guide you, it could get a little confusing for you, as we'll have a different approach. RE: Simple Question - ' defined as "a". ?' - Ryan012 - May-25-2023 Gribouillis : Thank for your patient and concerns. So What do I have to do from the original file as the following ? : import a as l def sum (x, y): return x + y # calls the sum function defined in the module a print (l.sum(2, 6)) Do I need to change the import statement? RE: Simple Question - ' defined as "a". ?' - deanhystad - May-25-2023 Is there a file named "a.py"? Is that file in a folder that is in sys.path? is there something named "l" in "a.py" These are terrible names, but even terribly named things can be imported if the paths and names are correct. RE: Simple Question - ' defined as "a". ?' - Larz60+ - May-25-2023 suggest you run through this: https://realpython.com/courses/python-imports-101/ |