Getting error "Module 'turtle' has no 'TurtleScreen' member" - python

I am getting an error while writing the Python program using the turtle module in VS Code:
"Module 'turtle' has no 'TurtleScreen' member"

Instead of the functional interface:
import turtle
wn = turtle.TurtleScreen()
Use the object-oriented interface:
from turtle import Screen, Turtle
wn = Turtle()
wn.TurtleScreen()

You have 2 problems.
First, you named your file turtle.py. This shadows the actual turtle module, and Python will load your turtle.py instead of the actual turtle built-in module.
You can easily check this by printing the module __file__:
import turtle
print(turtle.__file__)
wn = turtle.TurtleScreen
$ python turtle.Turtl.py
/path/to/your/turtle.py
...
Because if you rename turtle.py to something else (myturtle.py), it should print out a path to the Python installation folder:
$ python turtle.Turtl.py
/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/turtle.py
...
Second, what you are actually seeing in the VS Code Problems tab, is a pylint error about not finding the module you are importing. It's a common problem with VS Code and pylint. The fix depends on the module you are importing, but generally, you can tell PyLint where to look for modules.
One solution is making sure you activated or selected the correct Python environment in VS Code. I see you are using Python 3.9, so make sure VS Code is using that same Python installation as what you use to run your code:
Another solution is to add an init-hook option to VS Code's pylintArgs:
{
"python.linting.pylintArgs": [
"--init-hook",
"import sys; sys.path.insert(0, '/usr/local/Cellar/python#3.9/3.9.1_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/')"
]
}
Specify the path to the folder containing the module.

Related

Import on shared object fails with ModuleNotFound only when script is executed from command line

ran into a weird problem where there is a shared-object import error only when I run the script from command line. It succeed if i run the script in the python console using exec(...)
I have a class that needs a shared object: foo.py:
import os
cur_dir = os.curdir()
os.chdir('/tmp/dir_with_shared_object/')
import shared_object_class
os.chdir(cur_dir)
class Useful:
... # use the shared object import
Then there is a script like this:
from foo import Useful
If I enter python console and run:
exec(open('script.py').read())
Everything works fine.
If I run this on command line:
python script.py
I will get
ModuleNotFoundError: No module named 'shared_object_class'
The python is the same. It is 3.7.3, GCC 7.3.0 Anaconda. Anyone knows what is causing this discrepancy in behavior for shared object import?
A standard way of importing from a custom directory would be to include it in the PYTHONPATH environmental variable, with export PYTHONPATH=/tmp/dir_with_shared_object/.
update
It could also be done dynamically with
import sys
p = '/tmp/dir_with_shared_object/'
sys.path.insert(0, p)
PS
I think I have an explanation for why OP's original code didn't work. According to this python reference page, the import system searches, inter alia, in "[t]he directory containing the input script (or the current directory when no file is specified)." So the behavior in the REPL loop is different from how it is when running a script. Apparently the current directory is evaluated each time an import statement is encountered, while the directory containing the input script doesn't change.

Python module script.vis install

Really sorry for my novice question.
I am trying to install a module in python for neo4j but I got an error.
here is the import :
from scripts.vis import vis_network
from scripts.vis import draw
Here is the error:
ModuleNotFoundError: No module named 'scripts'
I have tried "pip install scripts"
Thanks in advance
ModuleNotFoundError simply means the Python interpreter couldn't find the module. I suggest that you read about python modules and packaging here.
I have looked at the source code you pointed to and it works perfectly fine. I suspect your paths are not well set up.
Make sure that in you are running importing scripts.vis in app.py, the directory structure looks like this:
./scripts
./scripts/__init__.py
./scripts/vis.py
....
./app.py #in app.py, you can import as 'from scripts.vis import x'
Here's what it looks on my system:
app.py is successfully able to make the import from vis sub-module. You can use a IPython notebook, that should work fine too.
If you want to visualize the graph in the python environment (Jupyter), you can try using neo4jupyter library. Here you will use neo4jupyter.draw to visualize the graph.
Install !pip install neo4jupyter
For example:
import neo4jupyter
neo4jupyter.init_notebook_mode()
from py2neo import Node
nicole = Node("Person", name="Nicole", age=24)
drew = Node("Person", name="Drew", age=20)
graph.create(nicole | drew)
options = {"Person": "name"}
neo4jupyter.draw(graph, options)
You may find this useful:
https://github.com/merqurio/neo4jupyter
https://nicolewhite.github.io/neo4j-jupyter/hello-world.html

pygal on windows - cannot access classes from pygal

I have such short script:
import pygal
if __name__ == '__main__':
bar_chart = pygal.Bar()
and following error: AttributeError: 'module' object has no attribute 'Bar'
Do you have any idea what is wrong? Shall I configure some additional paths? I am using windows.
Thank you
If your script is named pygal.py, when you import pygal, it's going to import your script, not the pygal library you installed into your system site-packages. And your script obviously doesn't have a class named Bar.
The solution is simple: rename your script to something different. Like pygaltest.py or mypygal.py.
And make sure to look at the directory and see if there's a pygal.pyc left behind, which Python compiled from your pygal.py. If so, you have to delete that file.

how to import python module inside IDE

I have this line of import in my python code:
from project.lib.framework.test_cases import TestCase
and it works fine when I run it from command line. However, if I try to run it from my IDE (Active state komodo), I get an error:
ImportError: No module named project.lib.framework.test_case.
Can anyone tell me why?
Because you changed the import statement?
In the first example, you are importing from project.lib.framework.test_cases, but in the second, you appear to be importing from project.lib.framework.test_case. Notice the missing s at the end.
Other then that, assuming you are using the same python binary, there should be no difference between an IDE and a command line for import statements.

Opening an EXE of my Pygame program gives me import errors

I made an MP3 player with pygame code:
from Tkinter import *
import pygame
import glob
import tkFont
songs=[]
for x in glob.glob('C:\WhaleTunes\Downloaded/*mp3'):
songs.append(x)
Admin=Tk()
num=0
plpa=-1
songas=Label(Admin,text='',bg='red')
songas.place(relx=0.0,rely=0.7)
def play(number):
pygame.mixer.music.unpause()
pygame.mixer.music.load(songs[number])
pygame.mixer.music.play()
songas.configure(text=songs[number])
def pause():
pygame.mixer.music.pause()
def Pre():
global num
if num == 0:
z = len(songs)
num=z
num+=1
num-=1
play(num)
def Next():
global num
num+=1
play(num)
#init pygame mixer
pygame.mixer.init()
#atach all buttons & labels
fons=tkFont.Font(family="bold", size=40)
fon=tkFont.Font(family="Helvetica", size=20)
tit=Label(Admin,text='Mp3 Player',font=fons,fg='grey',bg='red')
tit.place(relx=0.2,rely=0.0)
playnpause=Button(Admin,text='Play',command=lambda:play(num),fg='yellow',bg='red',font=fon)
playnpause.place(relx=0.0,rely=0.4)
last=Button(Admin,text='Previous',command=Pre,fg='yellow',bg='red',font=fon)
last.place(relx=0.2,rely=0.4)
first=Button(Admin,text='Next',command=Next,fg='yellow',bg='red',font=fon)
first.place(relx=0.5,rely=0.4)
pauses=Button(Admin,text='Pause',command=pause,fg='yellow',bg='red',font=fon)
pauses.place(relx=0.7,rely=0.4)
Admin.minsize(width=500, height=200)
Admin.maxsize(width=500, height=200)
Admin.configure(bg='red')
Admin.mainloop()
And I tried to put it into an exe with this code:
from distutils.core import setup
import py2exe
setup(console=['mp3player.py'])
When I run the mp3player.exe I get a bunch of import errors:
C:\Users\P'sao\Downloads\dist\mp3player.exe:2: RuntimeWarning: import display: N
o module named _view
(ImportError: No module named _view)
C:\Users\P'sao\Downloads\dist\mp3player.exe:2: RuntimeWarning: import draw: No m
odule named _view
(ImportError: No module named _view)
C:\Users\P'sao\Downloads\dist\mp3player.exe:2: RuntimeWarning: import image: No
module named _view
(ImportError: No module named _view)
C:\Users\P'sao\Downloads\dist\mp3player.exe:2: RuntimeWarning: import pixelcopy:
No module named _view
(ImportError: No module named _view)
C:\Users\P'sao\Downloads\dist\mp3player.exe:2: RuntimeWarning: import transform:
No module named _view
(ImportError: No module named _view)
Anyone know how to fix this?
And when I compile everything I get this error:
The following modules appear to be missing
['AppKit', 'Foundation', 'Numeric', 'OpenGL.GL', '_scproxy', 'copyreg', 'dummy.P
rocess', 'numpy', 'pkg_resources', 'queue', 'winreg', 'pygame.sdlmain_osx']
The solution is to add import pygame._view to the top of your main source file. Any of the packagers should work after that. I encountered this problem using cx_Freeze, py2exe, and pyInstaller. This is a serious bug affecting many of the exe packagers when attempting to package pygame programs.
Had the same problem before and found answer to solve it by myself:
After few weeks (had this problem even before) I'm happy to say that I solved this problem! :)
1st part of my problem (http://i.stack.imgur.com/WpkjR.png):
I solved it by editing setup.py script with adding "excludes" part in it. That resulted in successful making of executable file!
Modified setup.py script:
from distutils.core import setup
import py2exe
setup(windows=['source_static.py'], options={
"py2exe": {
"excludes": ["OpenGL.GL", "Numeric", "copyreg", "itertools.imap", "numpy", "pkg_resources", "queue", "winreg", "pygame.SRCALPHA", "pygame.sdlmain_osx"],
}
}
)
So, if you have similar issues, just put those "missing" modules into this "excludes" line.
2nd part:
After I succeeded in making of executable file, I had next problem: "The application has requested the Runtime to terminate it in unusual way. Please contact...". After days and days of searching and thinking how to solve this another problem, I found a way to do it. I couldn't believe that the problem was so absurd. The problem was in my code, with font definition:
font1 = pygame.font.SysFont(None, 13)
After changing "None" to some system font name (for an example "Arial" (must be a string)), and compiling, I couldn't believe that my .exe file worked!
font1 = pygame.font.SysFont("Arial", 13)
Of course, you can use your own font, but you must specify its path and define it in your program.
So for all of you who are experiencing this issues, try this steps and I hope that you will succeed.
I really hope that this will help you, because I've lost days and weeks trying to solve these problems. I even tried making my .exe file with all versions of python and pygame, with many other .exe builders and setup scripts, but without luck. Besides these problems, I had many other problems before but I found answers to them on stackoverflow.com.
I'm happy that I found a way to solve this problems and to help you if you are faced with the same ones.
Small tips (things I've also done):
1st: update your Microsoft Visual C++ library to the latest one.
2nd: if you have images or fonts similar that your executable program needs, include them to dist folder (where your .exe file has been created).
3rd: when you are making your .exe file, include all needed files to the folder where your setup.py script is (all files and directories that your main script uses).
Used Python 2.7 x64, pygame and py2exe.

Categories