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.
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
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.
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.
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.