Python Forum

Full Version: Simple Question - ' defined as "a". ?'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have one error for no defined as "a". How can we define "a"? Angel Blush

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))
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 a
Have a play with that and see if you can work it out.
(May-25-2023, 01:37 PM)Ryan012 Wrote: [ -> ]import a as l
The module «a» can be imported only if the file a.py is in a directory on the Python path.
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.
Python path means my main fils should be in the same directory. right?
(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.
(May-25-2023, 03:51 PM)Ryan012 Wrote: [ -> ]Hi rob101

Thank you so much for your kind response and explaination.

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.
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?
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.
Pages: 1 2