IDLE in virtualenv does not function correctly? - python

I installed numpy and other packages in virutalenv vpy1. I can correct import those packages if I invoke python command in console after activated the vpy1.
However, if I use IDLE, it cannot import those packages:
>>> import numpy
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import numpy
ImportError: No module named numpy
I then found out the IDLE does not have correct virtualenv setting. If I invoke python in console, the setting will be like this:
(vpy1)xxx ~ $ python
Python 2.7.6 (default, Jun 1 2014, 03:20:25)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.real_prefix
'/home/xxx/python-2.7.6'
>>> sys.executable
'/home/xxx/virtualenvs/vpy1/bin/python'
if I use IDLE:
(vpy1)xxx ~ $ which idle
/home/xxx/virtualenvs/vpy1/bin/idle
(vpy1)xxx ~ $ idle
#in IDLE
Python 2.7.6 (default, Jun 1 2014, 03:20:25)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> sys.executable
'/home/xxx/python-2.7.6/bin/python2.7'
>>> hasattr(sys,'real_prefix')
False
How can I fix this?

Finally solved this problem. In the vpy1/bin the idle is directly link to /home/xxx/python-2.7.6/bin/idle, which using a wrong shebang. Change the shebang to #!/usr/bin/env python solve this problem.

Related

Python and iPython submodule import differences

I recently migrated my solution using pytz to dateutil.tz. I've tested everything with iPython, and it worked nicely. However, when in production with python... ERROR!
Can anyone explain why Python and iPython deal differently with the imports, and if there is a way to make python behave the same way as iPython does?
$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type "help", "copyright", "credits" or "license" for more information.
>>> import dateutil
>>> dateutil.__file__
'/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
>>> dateutil.tz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'dateutil' has no attribute 'tz'
>>>
$ ipython
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import dateutil
In [2]: dateutil.__file__
Out[2]: '/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
In [3]: dateutil.tz
Out[3]: <module 'dateutil.tz' from '/usr/local/lib/python3.8/site-packages/dateutil/tz/__init__.py'>
Because IPython wants to be too clever. For any reason it has already imported dateutil.tz and when you manually import dateutil, then dateutil.tz is available in you main namespace.
But as is is a submodule, you should consistently import it if you want to use it. So you should replace import dateutil with import dateutil.tz and your code will work fine on both environment.
$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type "help", "copyright", "credits" or "license" for more information.
>>> import dateutil.tz
>>> dateutil.__file__
'/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
>>> dateutil.tz
<module 'dateutil.tz' from '/usr/local/lib/python3.8/site-packages/dateutil/tz/__init__.py'>

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.

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.

PyQt4 can't import Qsci

I'm installing PyQt4 on an old Linux system (CentOS 4.4) that can't be upgraded for hardware compatibility reasons. I've got Python 2.6, QT4 and SIP installed, and the installation of PyQt4 didn't give me any errors.
When I run Python, this happens:
Python 2.6.2 (r262:71600, May 11 2011, 14:18:37)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt4
>>> import PyQt4.Qsci
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Qsci
I looked around, and found /usr/local/lib/python2.6/site-packages/PyQt4/uic/widget-plugins/qscintilla.py, which is almost empty of non-comment stuff:
pluginType = MODULE
def moduleInformation():
return "PyQt4.Qsci", ("QsciScintilla", )
Any ideas?
You need to install qscintilla separately. If PyQt is already installed, then you should just have to install the qscintilla python bindings. Hopefully this fixes your issue!

Categories

Resources