Python Forum
Simple Question - ' defined as "a". ?'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Question - ' defined as "a". ?'
#1
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))

Attached Files

Thumbnail(s)
   
Reply
#2
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.
Ryan012 likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
(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.
Ryan012 likes this post
Reply
#4
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.
Reply
#5
Python path means my main fils should be in the same directory. right?
Reply
#6
(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.
Ryan012 likes this post
Reply
#7
(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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#8
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?
Reply
#9
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.
Reply
#10
suggest you run through this: https://realpython.com/courses/python-imports-101/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 206 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Very simple question about filenames and backslashes! garynewport 4 1,978 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,600 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,766 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  Simple arithmetic question ebolisa 5 2,082 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Simple Python script, path not defined dubinaone 3 2,713 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Simple code question about lambda and tuples JasPyt 7 3,373 Oct-04-2021, 05:18 PM
Last Post: snippsat
Big Grin question about simple algorithm to my problem jamie_01 1 1,696 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  Simple question 1234 4 2,301 Dec-04-2020, 12:29 PM
Last Post: DeaD_EyE
  Simple Timer Question cranberrica 3 2,231 Jun-22-2020, 06:29 PM
Last Post: cranberrica

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020