psutil can not be found within a flask project - python

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.

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

python import works interactively but not from script

Disclaimer: I'm not a Windows expert, so this may be something idiosyncratic to Windows.
I'm observing a situation where I can import a module in interactive mode but not when requested from a file. Here is foo.py in its entirety:
#!/usr/bin/env python
import pyftdi
When I try to run this in Python 3.6.4, it fails:
E:\>py foo.py
Traceback (most recent call last):
File "foo.py", line 2, in <module>
import pyftdi
ImportError: No module named pyftdi
...but when I import the module interactively, it succeeds:
E:\>py
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyftdi
>>>
(FWIW, I don't get an error when repeating this under Mac OS X.)
Any thoughts about what's going on?
The problem is most likely that the version of python is different and has different modules installed. To fix this you can simply do py -3.6 foo.py instead of py foo.py.
Working directory
I had a situation where the script was importing from it's working directory and the shell was importing from the global libraries.

No module names django.core, but I can import django from the shell

When I run django in a virtual environment, I get No module named 'django.core':
(proj)[jenia#li app]$ python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named 'django.core'
However, when I run the Python interpreter, and import django, there's no error message:
(proj)[jenia#li app]$ python
Python 3.4.1 (default, May 19 2014, 17:23:49)
[GCC 4.9.0 20140507 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import django
>>>
When I import django.core it fails of course.
What am I doing wrong? What could this be related to?
Thanks in advance?
I think you have no django installed at all or you have similar called folder or py-file in current directory.
You can print django.__path__ and I bet it will point to some script, called django.py or package folder django, which is not a django.
You should rename it, so import will look deeper, to python-packages, where real django is located.

Missing module in 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!

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