Getting "ModuleNotFoundError" trying to import Tkinter Canvas? - python

I'm trying to do the same thing Marc Leese tried to do here but didn't seem to get a clear answer on (at least none that works for me): trying to convert this file to python 3 and getting the error module not found
That is, use in a Python 3+ environment the solitaire.py example code as found e.g. here: https://android.googlesource.com/toolchain/python/+/0d4194853e08d3244931523470331c00dfb94863/Python-2.7.3/Demo/tkinter/guido/solitaire.py
Specifically, I'm running Python 3.7.9 through Spyder 3.3.6.
When I try to run the above code, I first get an error that Tkinter is not found. When I correct Tkinter to tkinter, however, I still get an error that says:
ModuleNotFoundError: No module named 'Canvas'
Regardless of whether I use from tkinter import * or from tkinter import Canvas I keep getting the same error.
The whole code block in question is:
# Imports
import math
import random
from tkinter import *
from Canvas import Rectangle, CanvasText, Group, Window
# Fix a bug in Canvas.Group as distributed in Python 1.4. The
# distributed bind() method is broken. Rather than asking you to fix
# the source, we fix it here by deriving a subclass:
class Group(Group):
def bind(self, sequence=None, command=None):
return self.canvas.tag_bind(self.id, sequence, command)
I've noticed before that example code using import * did not seem to work as in the example, but before I've always managed to find a workaround.
If someone could explain what's going from here/how to fix it I'd be very much obliged!

You've copied some very old code that apparently depends on a module named "Canvas" (for example, a file named Canvas.py). This module is not part of tkinter. You will need to find the original Canvas.py file

Related

Python Module not found error - not getting fixed

So I am pretty new with Python. I've been working on running with a code similar to one I hope to build myself (so a reference code). Aside from some bugs I need to work out with invalid syntax, all seems to work except for one issue with one particular .py file I have.
My structure is this:
MoodForecasting -> eval -> nsga_two.py
I do have _init_.py in eval folder though, so I'm not sure why this block of code isn't working.
I am trying to load one particular fucntion from it, so the structure should look like this
from nsga_two import PatientProblem
Unfortunately, I keep getting the error ModuleNotFoundError: No module named 'nsga_two'.
I checked nsga_two.py itself and found that it couldn't load inspyred. I went in and was able to fix this. So, nsga_two.py runs fine on its own. However, I cannot import it into the main script I will be working with.
Some extra details: I am working with the IDE Spyder with Python Custom Version 3.7.9.
I'm not sure if it is an issue with Spyder or just how I am loading in my working directory. (Most of my coding experience is in MatLab and R so having an IDE similar to RStudio and MatLab is the reason I chose to work in Spyder)
Edit:
I got a syntax error when using from eval import nsga_two.PatientProblem. Python didn't like the period. So, I instead tried it with no period. I got the error cannot import name 'nsga_twoPatientProblem' from 'eval' (C:\Users\name\Desktop\MoodForecasting-master\MoodForecasting-master\eval\__init__.py). I don't know why. But doing from eval import nsga_two works. nsga_two.py only consists of PatientProblem. This solve should be ok for this purpose. I'm just not sure why this could be happening.
Suppose your structure is like:
MoodForecasting-master/
main.py
eval/
__init__.py
nsga_two.py
When you run the main script to import something, the directory of that script is added to the module search path sys.path, .../MoodForecasting-master/ in this case. from nsga_two import PatientProblem raised ModuleNotFoundError because nsga_two.py is not in that directory.
As Iguananaut said, from eval import nsga_two.PatientProblem in the first comment has never been a valid statement. The valid ways of doing so are:
import by from eval import nsga_two and use as nsga_two.PatientProblem().
import by from eval.nsga_two import PatientProblem and use as PatientProblem() directly.
Module search starts from .../MoodForecasting-master/, first option go to .../MoodForecasting-master/eval/ to find nsga_two.py, second option go to .../MoodForecasting-master/eval/nsga.py to find attribute named PatientProblem.
The correct syntax would be:
from package.module import function
so:
from eval.nsga_two import PatientProblem

how do i correctly import a package in python?

Hello everyone im currently learning python and i im having some problems importing modules and packages. Actually i think is more of a problem with vscode.
i have this package called "paquete" with a module (funciones) that i want to import to my "main" with some fuctions in it to test if it all works correctly but i still getting "emphasized items and unresolved-import" warnings.
but for some reason it works just fine.
is more of a annoying thing.
EDIT:
module with the function "funcion"
the warning that appears in the main folder "prueba" is "emphasized items"
i tried what u guys told me to do but it stills shows the warnings
As you are trying to import a specific function from module in python
You should use in this manner:
from paquete import funciones
If you want to import full module then use:
import paquete
I can't tell whats in the funciones file. But normally this yellow import lines are telling you that you import functions, which you dont use.
Try this instead if you only want
funcion
to be imported.
from paquete.funcions import funcion
This is also better because you import only the functions you need, not all of the functions you declared in the other file. Also all imports of the other file will be loaded into your file if you import with an asterix.
The issue is you are doing all of this from within a directory named prueba. If you changed the import to from prueba.paquete.funciones import * it should work after you add a __init__.py file to your prueba directory. The other option is to use a relative import: from .paquete.funciones import *.
But do note that using import * is strongly discouraged when you are not working within the REPL. It's much better to import to the module and then reference things off the module, e.g. from prueba.paquete import funciones, from .paquete import funciones, or import prueba.paquete.funciones. That way you know exactly where things in your code came from without having to read the top of your file.
pip3 intall "name"
Use Pycharm, rather than Vscode

In python, why do you have (seemingly) to import some libraries twice?

I've browsed the similarly phrased questions, but haven't found an answer:
When importing SOME libraries, it seems like I have to import the top level library in one statement, and then import the module or object I want to use in a second statement. An example that I have recently come across is:
import tkinter as tk
import tkinter.filedialog
Which results in me being able to get a file dialog in two different ways:
files = filedialog.askopenfilenames()
or
files = tk.filedialog.askopenfilenames()
Why can I not call the second option without importing tkinter.filedialog? I'm not familiar with the specific structure of files/packages/modules/ etc. I assume it has something to do with it.
EDITED: You can check out these answers for your question:
Python 3.6 - AttributeError: module 'tkinter' has no attribute 'filedialog'
Why tkinter module raises attribute error when run via command line but not when run via IDLE?

Importing packages works but still get errors?

I have a Python application that conforms to the MVC model so I have folders called Controller, Model and View. In each of these folders I have Python files that I import into each other. It seems to work however when you hover the cursor over the import I see the "Unable to import XXXX" error and I am not sure why this is.
I have an empty __init__ file in each folder and have added the path to system path.
The directory of my application goes like this:
Desktop/Application--¬
Controller--¬
MainController.py
GraphEngine.py
APIMethods.py
Model-------¬
MainModel.py
DatabaseAccess.py
View
I use import sys
sys.path.append('C:/Users/XXXX/Desktop/Data-Processing-Engine-Sorted/Controller')
to set the system path and then use import MainController to actually gain access to the file.
This seems to work fine. The other file locates MainController and uses its methods with no issue, however in the IDE I can't get rid of the "Unable to import MainController" error. I am really quite confused as to why.
Thanks for your help in advance.
I found out the issue! It's very simple and quite embarrassing...
When calling one of the methods I was only calling the module rather than the class, for example:
controller = MainController() Would return with an error while
controller = MainController.MainController() Would create an instance of my class

Python "import random" Error

As you may know from my previous posts, I'm learning Python. And this time I have a small error which I think is with this build of Python itself. When using the following:
import random
number = random.randint(1,10000)
Python gives me this error:
File "C\Users\name\Documents\Python\random.py", line 5, in (module)
print random.random()
TypeError: 'module' object is not callable
Every time I try to run it. Me no understand. Any help would be much appreciated!
EDIT: The two lines of code I'm trying to run:
import random
print random.randint(1,100)
That's it. And it gives me the same error.
By naming your script random.py, you've created a naming conflict with the random standard library module.
When you try to run your script, the directory containing the script will be added to the start of the module import path. So when your script does import random, you're effectively running a second copy of the script as the random module.
When the random module runs import random, it means that random.random will also be a reference to your module. So when you attempt to call the random.random() standard library function, you're actually attempting to call the module object resulting in the error you got.
If you rename your script to something else, the problem should go away.
Even I faced the same problem. I have renamed my python file from random.py to shuffle.py. This didn't work out. Then I changed the version then it worked. This may help a bit.
Python version : 3.6.7
replace
import random;
to
import random2;
I am using pycharm and I had to take the additional step to import the methods from random. In my case:
import random
from random import choice
The simple answer: Change your filename from "random.py" to something else as it is conflicting with random library.

Categories