python import works interactively but not from script - python

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.

Related

distutils.spawn not available unless imported

I have distutils installed and it works in some cases. However when trying to use a submodule it won't import unless I explicitly import it.
$ python
Python 3.8.5 (default, Jul 21 2020, 10:42:08)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import distutils
>>> distutils.spawn
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'distutils' has no attribute 'spawn'
>>> from distutils import spawn
>>> distutils.spawn
<module 'distutils.spawn' from '/usr/local/Cellar/python#3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/distutils/spawn.py'>
I'm on Mac and have tried inside a venv and outside of one. I have a dependency that calls distutils.spawn.find_executable('python3') and isn't working.
This is perfectly normal. This is how Python import works. Unless distutils/__init__.py imports .spawn itself (which it doesn't do) you have to import it yourself to make it available. Importing just distutils is not enough to access submodules.
Counterexample: import os makes os.path automatically available but that's because os.py makes it available for you.

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.

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