Missing module in python - python

I recently tried installing powerline-vim, but have been running into problems with it.
Every time I open a new window, I see this error:
Error detected while processing function <SNR>9_UpdateWindows..<SNR>9_pyeval:
line 1:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 1, in <module>
File "opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/uuid.py", line 545, in uuid4 import random
File "opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/uuid.py", line 545, in uuid4 from os import urandom as _urandom
ImportError: cannot import name urandom
When I delete this line from my .vimrc file (effectively disabling powerline-vim) the error goes away.
python from powerline.ext.vim import source_plugin; source_plugin()
The curious thing about this is when I fire up python from the terminal, the imports work just fine.
Python 2.7.3 (default, Nov 17 2012, 19:54:34)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> from os import urandom as _urandom
>>>
The output of which python:
/opt/local/bin/python
I am running OS X 10.8.2.
Thanks!

Check if your problem is related with that: Python: cannot import urandom module (OS X). Check your sys.path value. Make sure you import os module from your installation of Python, not from system Python.
Check the log configure generates when you build Python for clues. Have you specified prefix when building Python?
Edit the code and print os and sys.path just before the error occurs to check if the plugin changed something. If sys.path is modified, often you may find the place where it is by performing import sys; sys.path = tuple(sys.path). It does not crash on sys.path = smth, but crashes on append() and +=. Maybe that would be enough to show the place where path is modified.

The problem was I was executing the wrong version of Python.
sudo port select python python27-apple
Running that line fixed it.
Thanks!

Related

How to import a package that use absolute import in python

I'm trying to import volatility3 into my python project/script, so that I don't have to use os.system since volatility3 is already made in python3.
I'm wondering how can I import all the functions/modules of said project ? The functions I'm interested in are located in volatility3/volatility/framework
I tried simply putting:
>>> import volatility3.volatility.framework
But I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/volatility3/volatility/framework/__init__.py", line 12, in <module>
from volatility.framework import constants, interfaces
ModuleNotFoundError: No module named 'volatility'
My guess is I have to modify sys.path or one of the path variables but this does not seem to work.
Thanks,
The best solution here would be to properly install volatility with pip3, from your already exiting repository folder:
$ pip3 install /home/volatility3
or directly from pipy (not tested):
$ pip3 install volatility3
then you should be able to import the from volatility package directly:
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import volatility.framework
>>> volatility.framework
<module 'volatility.framework' from '/home/bruno/.local/lib/python3.6/site-packages/volatility/framework/__init__.py'>
>>>

random module error when run as .py

It does not load the specific module of random when you run it as a .py. But if I load it in the python shell, I have no problems.
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import random
print(random.randint(0,9))
I run it... and:
Traceback (most recent call last):
File "random.py", line 4, in <module>
import random
File "C:\Users\root\Pythin\random.py", line 5, in <module>
print(random.randint(0,9))
AttributeError: module 'random' has no attribute 'randint'
And in the python shell I have no problem:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> print(random.randint(0,9))
1
I,m running it in Windows PowerShell (Windows10|64bits). Using the command python random.py. Someone gives me a hand? Thank you very much.
Change the file name of your code to "myrandom.py". And then run the program.
Update: Please don't name your Python source code file to standard modules. As it will cause error. Because right now instead of importing the standard library RANDOM of python, the python imports this random.py and which causes the error.
e.g. Do not give your python source code following names etc.:
random.py
time.py
csv.py
etc... ... ... ....
You can try re-naming the file and try again
Never name your files as random.py.

psutil can not be found within a flask project

I have setup a fairly simple flask project. Within this flask project I want to use psutil but its not being found with the import statement.
> $ ./satelite.py
Traceback (most recent call last):
File "./satelite.py", line 2, in <module>
from app import app
File "/home/neil/monitor/satelite/app/__init__.py", line 4, in <module>
from app import views
File "/home/neil/monitor/satelite/app/views.py", line 6, in <module>
import psutil
ImportError: No module named psutil
However when I use python cli it is.
> $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> print psutil.cpu_percent()
15.3
The same can be said for just a straight forward python script being executed separately.
Standard python modules(subprocess, os, etc) are loading fine. I have tried to focus my google-fu on this but I am getting nowhere so would massively appreciate if anyone can point me in the right direction.
Cheers
Does your first line in satelite.py refers to the same python binary as which python in your terminal ?
(This refer to the #! line)
Maybe you are using python3 in your satelite.py file.

ImportError in IDLE

I consider myself an intermediate user in python, and this one is a new one. Testing code in IDLE (Python 3.2.3) on Linux. Here is the entire script:
Python 3.2.3 (default, Apr 10 2013, 05:29:11)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>> from os import listdir, getcwd, chdir
>>> chdir('Documents/matrix')
>>> getcwd()
'/home/bradfordgp/Documents/matrix'
>>> listdir('.')
['__init__.py', 'vec.zip', 'hw1.pdf', 'politics_lab.pdf', 'submit_hw1.py', 'submit_politics_lab.py', 'test_vec.py', 'Week1', 'Week0', 'python_lab.py~', 'Week2', 'vec.pdf', '__pycache__', 'hw1.zip', 'politics_lab.zip', 'voting_record_dump109.txt', 'my_stories.txt~', 'hw1.py', 'politics_lab.py', 'submit_vec.py', 'vec.py']
>>> from vec import Vec
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
from vec import Vec
ImportError: No module named vec
>>>
I've navigated to the correct directory, and I'm importing from the same directory, and vec.py exists. Why is in not locating a file in the local directory? Running this script from a terminal window command line works correctly.
Suggestions?
use this..
import sys
sys.path.append("/home/bradfordgp/Documents/matrix")
import vec
import sys
sys.path.append("/home/bradfordgp/Documents/matrix")
import vec
Rather than change to the directory, just add the location to the places python will search on import
See more here
In interactive mode, import will try to import modules from the current directory after os.chdir. But in non-interactive mode, it will fail and still search from the previous directory. You can see more discussion from this issue. In non-interactive mode, you'd better do what others mention.

Why is this module not being found when I run the script but is in the interpreter?

I'm trying to import the tkinter module into my script. I have it installed since the interpreter is able to import it with no problems:
C:\Users\Nacht\Dropbox\Scripts>python
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
Imports fine, no problem. But now when I try to run a script that has the line import tkinter....
C:\Users\Nacht\Dropbox\Scripts>t ls
Traceback (most recent call last):
File "C:\Users\Nacht\Dropbox\Scripts\t.py", line 5, in <module>
import tkinter
ImportError: No module named tkinter
where t is the name of the script and ls a command (it is a command-line interface).
How can the interpreter see it but not the script? Thanks.
EDIT:
The sys.path says, for the interpreter:
C:\Python32\lib\site-packages\distribute-0.6.24-py3.2.egg
C:\Python32\lib\site-packages\selenium-2.15.0-py3.2.egg
C:\Windows\system32\python32.zip
C:\Python32\DLLs
C:\Python32\lib
C:\Python32
C:\Python32\lib\site-packages
C:\Python32\lib\site-packages\win32
C:\Python32\lib\site-packages\win32\lib
C:\Python32\lib\site-packages\Pythonwin
and for the script:
C:\Users\Nacht\Dropbox\Scripts
C:\Python27\lib\site-packages\distribute-0.6.24-py2.7.egg
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info
The script appears to be running with Python 2.7 but when you run the interpreter directly, it is using Python 3.2. As mentioned by #DSM, the name of Tkinter was different (perhaps there are other differences?).
May be there is a problem with path. It can't find the tkinter module. Setting up correct path try to import again.

Categories

Resources