Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Import" Help
#1
I'm currently taking a free online course on Python and I am admittedly a beginner in Python as well as coding in general, but I am having an issue that I cannot seem to resolve and hoping for some help.

The most recent assignments revolve around importing modules and building programs using those modules. For some reason, all of the modules that come pre-installed and most of the modules I've installed using pip seem to work just fine, except for a handful. The primary ones I'm having issues with are the "inflect" and "pyfiglet" modules. I've installed them using pip, and the system says they were installed correctly.

The "inflect" assignment implies to begin the assignment in this fashion:

import inflect
p = inflect.engine()
But it does not register inflect if I attempt to run any program using that setup, and none of the commands that are supposed to come along with it seem to work. Similarly, the "figlet" assignment implies to begin the assignment in this fashion:

from pyfiglet import Figlet

figlet = Figlet()

pyfiglet.getFonts()
figlet.setFont(font=f)
But again, the system does not register "pyfiglet" or "Figlet" or really anything that is supposed to come along with that module.

I have tried updating the modules, reading the instructions for the modules online, and cannot seem to find any way to resolve this issue. I'm not sure if I'm just making a boneheaded mistake, the modules are out-of-date or if I'm missing something else entirely. Hopefully someone can help me because I am lost on how to resolve this.
Reply
#2
There is a problem with this:
from pyfiglet import Figlet
 
figlet = Figlet()
 
pyfiglet.getFonts()
figlet.setFont(font=f)
You did not import pyfiglet. You only imported Figlet. The "figlet = Figlet()" probably works. "pyfiglet.getFonts()" would fail and cause a crash with a NameError because you never assigned a value to a variable named "pyfiglet.

That is what import does, your know. Yes, it pulls in modules and compiles them to byte code, but it also creates objects and assigns them to variables. import inflext loads the inflect module, but it also creates a variable named "inflext" to which it assigns a module object. In your pyfiglet example you never did "import pyfiglet". There was no variable named "pyfiglet" created and no module object created. "from pyfiglet import Figlet" creates a variable named Figlet and assigns pyfiglet.Figlet (probably a class) to the variable.

This should work.
import pyfiglet
from pyfiglet import Figlet
 
figlet = Figlet()
 
pyfiglet.getFonts()
figlet.setFont(font=f)
I don't know why you would have a problem with inflect. Can you post a program that uses inflect and has an error. Also post the error message and error trace.
Reply
#3
(Apr-28-2023, 02:10 AM)deanhystad Wrote: There is a problem with this:
from pyfiglet import Figlet
 
figlet = Figlet()
 
pyfiglet.getFonts()
figlet.setFont(font=f)
You did not import pyfiglet. You only imported Figlet. The "figlet = Figlet()" probably works. "pyfiglet.getFonts()" would fail and cause a crash with a NameError because you never assigned a value to a variable named "pyfiglet.

That is what import does, your know. Yes, it pulls in modules and compiles them to byte code, but it also creates objects and assigns them to variables. import inflext loads the inflect module, but it also creates a variable named "inflext" to which it assigns a module object. In your pyfiglet example you never did "import pyfiglet". There was no variable named "pyfiglet" created and no module object created. "from pyfiglet import Figlet" creates a variable named Figlet and assigns pyfiglet.Figlet (probably a class) to the variable.

This should work.
import pyfiglet
from pyfiglet import Figlet
 
figlet = Figlet()
 
pyfiglet.getFonts()
figlet.setFont(font=f)
I don't know why you would have a problem with inflect. Can you post a program that uses inflect and has an error. Also post the error message and error trace.

That worked perfectly, thank you for your help. As far as the inflect error, I have no idea what happened but it just started working and I was able to complete the assignment. Thank you again for the help!
Reply


Forum Jump:

User Panel Messages

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