Import error in Python 3.6 with sudo? - python

I have written a program that uses Scapy. Python is able to import the scapy module perfectly but using sniff function of scapy requires running the program as administrator.
However, running the program as sudo python3 <program_name> produces an import error, why is it so?
Here is the import line : import scapy.all as scapy

As using python3, I would recommend
sudo python3 -m pip install scapy
Of course pyenv works too I'd you're familiar with it.

You can fix it with:
$ sudo pip install scapy
And then try again.
I would recommend using virtualenv to run your program though. Instead of installing package after package in your main environment.

Related

Python Libraries installing to Python 2.7 by default

I recently shifted to a Macbook. I installed Python 3.8 and installed Pyperclip using pip install pyperclip in the terminal, which was successful. When I tried to import it in IDLE shell, I get the error
ModuleNotFoundError: No module named 'pyperclip'.
On looking around I found that pyperclip has been installed in-
/Users/aamodpant/Library/Python/2.7/lib/python/site-packages/pyperclip
I did not install Python 2.7 but it must be required for macOS, so I don't want to delete it. How do I change the "default" version of Python for installing new libraries?
Use :
python3 -m pip install pyperclip
This will install the package for python3.
You can reinstall or upgrade pyperclip using sudo:
pip3 install -U pyperclip
Also, run your script using python3 instead of just python:
python3 script.py
The best is to use alias, as this answer describes changing default Python version in Mac can cause multiple issues.
alias python=python3
alias pip=pip3
The answer you are probably looking for is You should not change this.
Otherwise, you may like to set Python 3 as default, there are many ways to do it. But do it with caution as the guides suggest below.
The official documentation: Using Python on a Macintosh, some well described very good guides: The right and wrong way to set Python 3 as default on a Mac, Installing Python 3 on Mac OS X.

Can not import PySerial even though it's installed

I'm trying to import PySerial import serial, but i get a ModuleNotFoundError: No module named 'serial'. I installed PySerial via conda install pyserial and I also tried with pip install pyserial in both cases I get the same error, but if execute conda list or pip list pyserial appears in the list. I'm using VS Code on Windows and unistalled and reinstalled Anconda, VS Code and PySerial serveral times. I checked also, that there is not the serial package.
Can anybody tell me why I can not import serial?
Edit:
I also tried conda install -c conda-forge pyserial once...
Probably you are not running the python version you think you are.
Find full path of the Python interpreter?
try:
import sys
print(sys.executable)
And see if it matches what you expect.
If you don't know what to expect, it's more likely you aren't running the right pip. In that case you could make sure to run the right pip like this:
python -m pip install pyserial
Which pip is with which python?

Why Python library import doesn't work via NRPE

I'm developing plugin Nrpe in Python.
When i try to execute my code it works well on my machine. But when the NRPE execute it, an error occurs : 'No Module named pycurl'
I'm working on CentOs6.10 with Python 3.4 and i've installed pycurl with easy-install, the path to pycurl.py is /usr/lib/python3.4/site-packages//usr/lib/python3.4/site-packages/pycurl.py
And my PYTHONPATH = ['/usr/local/bin', '/usr/lib64/python34.zip', '/usr/lib64/python3.4', '/usr/lib64/python3.4/plat-linux', '/usr/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages']
I hope somebody could help me ?
Thanks !
You probably need something like:
python3 -m pip install pycurl
You may want to use: python3 -m pip install --user pycurl
or a virtual environment.
pycurl doesn't come with python. You have to install it with pip from pypi.

Trying to install a pack from source. I get: ImportError: No module named requests Yet the pack is installed

I'm trying to install: https://github.com/rozzac90/pinnacle.git from source.
If I type sudo python setup.py install (or sudo python setup.py configure) I get:
import requests
ImportError: No module named requests
The module is installed, I'm sure. It is in the list of conda installed packs. Also, if I try to import it in other scripts, and
python
>>> import requests
>>>
with both python 2 and python3. I get no errors.
I also removed it and installed it again. Same results.
I am using Anaconda with a python3 environment. (I get the same errors in python 2, though.) So I can't use pip commands. I'm running a lubuntu machine, latest version.
Ideas anyone? Thanks in advance !
A suggested answer :
The solution was to run the script in the folder where setup.py is located, in general :
Try with
sudo /[annaconda_python_path]/python setup.py install
Make sure the right environment is active in the terminal.

PyCharm does not import Scapy module

I am trying and failing to import scapy into python, which I have now been playing with for hours and all the google hits I get seem to make sense but when I try them - it fails.
first off I am using python3 and have that nicely in pycharm:
Correct Interpreter
I have also installed the package using pip3:
pip3 install scapy-python3
this appears in PyCharm as you can see in the picture. So I am now on a par with the other guides I found.
So finally I try:
from scapy import *
and for good measure
from scapy.all import *
which is for Python2 and 3k from what I read.
Could someone give me a hand, I don't know where to go next.
Gareth
Scapy-python3 is a different and outdated project.
The correct way to install scapy on python 3 is
pip3 install scapy
Or
python3 -m pip install scapy
In some cases it is better to install scapy package for python2, too; by following command:
pip install scapy
try this.

Categories

Resources