Home > OS >  problem about importing a module into python
problem about importing a module into python

Time:02-03

I am working on packets in my linux iptable , so i need to work over them in python. To get the packets in my iptable into python , i decided to use netfilterqueue.However , i run into error when i want to use it. The error is : ModuleNotFoundError: No module named 'netfilterqueue'

However , this module is installed.

I followed up each process in its webpage. I have GCC C compiler , i installed "apt-get install build-essential python-dev libnetfilter-queue-dev" , and "pip install NetfilterQueue".

When i run the "pip install NetfilterQueue" ,second time , it says that :

$ pip install NetfilterQueue

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: NetfilterQueue in /home/salmon/.local/lib/python3.10/site-packages      (1.0.0)

When i go to the directory , i see that i have "netfilterqueue " and "NetfilterQueue-1.0.0.dist-info" directories , so i managed to download them.

I also download it in Pycharm without any problem. However , when i want to import it into my codes , it gives the error above.

I tried to restart both my main computer and virtualbox , but nothing is changed.

Can you help me fix this situation. I use Python 3.10.9 , pip 23.0 ,and vmware16 as virtualbox ,Kali Linux 2022.4

IMPORTANT ADDENTUM: When i open python using terminal , i can import it successfuly. I can see the inside of it using "dir()" function and learn about functions using "help()" function. However , when i try to run my code , i encounter with mentioned error ,thats ,importing the module error.

>>> import netfilterqueue
>>> dir(netfilterqueue)
['COPY_META', 'COPY_NONE', 'COPY_PACKET', 'NetfilterQueue', 'PROTOCOLS', 
'Packet', 'VERSION', '__builtins__', '__cached__', '__doc__', 
'__file__', '__loader__', '__name__', '__package__', '__path__', 
'__spec__', '__version__', '_impl', '_version']

>>>print(netfilterqueue)
 <module 'netfilterqueue' from '/home/salmon/.local/lib/python3.10/site- 
   packages/netfilterqueue/__init__.py'>

As you see , the module is used in terminal , but when i try to run the code using "sudo python 'filename'" , it gives " ModuleNotFoundError: No module named 'netfilterqueue'"

CodePudding user response:

This is the operative part that was missing in your original post:

but when i try to run the code using "sudo python 'filename'"

That's because your users python env is different from root's.

root != salmon

One way to deal with this would be to do the pip install ... as root, system-globally.

Alternatively you could try to preserve your own environment when doing the sudo bit ...

sudo -E python 'filename'

  • Related