Cannot run Python script using sudo - python

I have a simple script which is using signalr-client-py as an external module.
from requests import Session
from signalr import Connection
import threading
When I try to run my script using the sudo python myScriptName.py I get an error:
Traceback (most recent call last):
File "buttonEventDetectSample.py", line 3, in <module>
from signalrManager import *
File "/home/pi/Desktop/GitRepo/DiatAssign/Main/signalrManager.py", line 2, in <module>
from signalr import Connection
ImportError: No module named signalr
If I run my script typing only python myScriptName.py it works perfectly fine but I need to have the sudo in front because later on in my other scripts (that use this one) I perform write operation on the File system.
I am quite new to Python and that's why I need to know how I can handle this situation. If I type pydoc modules I get a list which contains:
signalr
signalrManager
If I type pip freeze I can see there listed:
signalr-client==0.0.7

By default sudo runs commands in different environment.
You can ask sudo to preserve environment with -E switch.
sudo -E python myScriptName.py
It comes with it's own security risks. So be careful

You need to check where signalr is installed. sudo runs the program in the environment available to root and if signalr is not installed globally it won't be picked up. Try 'sudo pip freeze' to see what is available in the root environment.

Another easy solution can be installing required packages via sudo -even they are installed before normally- instead of trying to match the paths:
sudo pip3 install <your-required-package-name>
After that you can execute the scripts via sudo.

Related

VS Code is not recognizing 'nmap'

I am trying to make a nmap scanner for the InfoSec Certification on freeCodeCamp.org and cannot get Visual Studio Code to recognize that I have installed nmap. I am very beginner and in the process of learning.
from cProfile import run
import nmap
scanner = nmap.PortScanner()
print("Welcome, this is a simple automattion tool")
print("<------------------------------------------->")
When I run this in VS Code I get the following in the terminal:
PS C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1> python3 scanner.py
Traceback (most recent call last):
File "C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1\scanner.py", line 2, in <module>
import nmap
ModuleNotFoundError: No module named 'nmap'
PS C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1>
I have so far:
Updated to the current Python 3.10.7
Installed Nmap the first time from https://nmap.org/ for Windows
Uninstalled Nmap
Reinsalled Nmap using >>>pip3 install python-nmap
For future searchers (as in my comment above), if you installed a module with pip3 but are still getting module import errors, try python3 -m pip install <module-name>.
Not sure how/why this happens (pip3 installing somewhere that python3 is not looking for modules - maybe PYTHONPATH-related), but the above can [usually] help.
It seems that Python cannot find nmap module.
You can either add it to path in Windows via Start -> Edit system environment variables -> Environment variables... and then edit PATH VARIABLE by adding at the end path to nmap module that you have installed or move the module to default Python modules directory which should be C:\Users\YourLogin\AppData\Local\Programs\Python\Python310\Lib\site-packages

I have a problem when I try to run python command in bash

I'm trying to run this command in bash: python3 HelloServer.py (HelloServer.py is the python file I want to run). It returns
["bash: python3: command not found"][1]
and if i run the following command...
python HelloServer.py it returns
"Traceback (most recent call last):
File "HelloServer.py", line 7, in <module>
from http.server import HTTPServer, BaseHTTPRequestHandler
ImportError: No module named http.server"
this is the bash:
this is the HelloServer.py code
Based on that module, it looks like the file you're trying to run is meant for python3 which you don't seem to have installed. Easily remedied though: https://www.python.org/downloads/
There are two possibilities, first is that as indicated by Bernard you don't have installed python 3, the second is that you do have python 3 installed but you are using python 2.7
It seems you don't have python3 installed. Try which python3 in your shell (Linux). The http.server module is for python3 only. If you want to stick to python2, SimpleHTTPServer might be what you want.
I seems that you haven't python3 on your system.
You need to install python3 first.
apt-get install pyhton3
After this you could write command in python3

python3 - ImportError: No module named twython

I am using python3.
I installed twython on my MAC using pip3 command and I confirmed it was successfully installed.
When I run my python file, it comes up with:
ImportError : No module named twython
My code is as follows:
import sys
import string
import json as simplejson
from twython import Twython
I can't comment the response from #ajxs, but as additional information to his repsonse:
You can change the default python interpreter like this in your MAC terminal:
nano ~/.bash_profile
Add this to the file:
alias python=python3
exit the the bashrc file and run the following command:
source ~/.bash_profile
Now you can check the defaul python version with:
python --version
Maybe this helps you.
First thing that springs to mind is to check that you're running the script with the correct version of Python. Use python --version on the command line to check which version of Python you're executing by default. I've definitely had problems like this before when I've forgotten that my system's default version was 2.7 and I needed to use python3 to run Python 3 on the command line.

Error to launch IPython shell after source install

I have an account in a CentOS server without sudo permission. As a result, I tried to install IPython from source code by python setup.py prefix=<my home directory>.
However, I got the Error:
Traceback (most recent call last):
File "/usr/bin/ipython", line 19, in <module>
from IPython.frontend.terminal.ipapp import launch_new_instance
ImportError: No module named IPython.frontend.terminal.ipapp
I found a question same to mine: IPython import failure and python sys.path in general.
I followed the asker's instruction to add the directory of my IPython to the IPython execution file.
#!/usr/bin/python
"""Terminal-based IPython entry point.
"""
import sys
sys.path.append("./Ipython directory")
from IPython.frontend.terminal.ipapp import launch_new_instance
launch_new_instance()
However, I got the same error as well. So I want to know how can I launch IPython correctly? Thanks.
You probably want to install with the --user flag, which should put it on sys.path automatically.
python setup.py install --user
If you need to use --prefix for some other reason, then make sure that the directory you add to sys.path is the folder containing the IPython package, not the IPython directory. It will probably end with 'site-packages'.
I copied the IPython source folder into ~/bin/. The execution file of IPython can recognize the module in IPython and the IPython shell launches successfully.

ImportError with VirtualEnv

I'm trying to use VirtualEnv for a Flask app that I'm creating since everyone has recommended me to do so. After creating my virtual environment, I installed the libraries that I needed using pip while the environment was activated. I'm running into ImportError problems with this script. The code works fine when I'm not in the virtual environment.
My script:
#!/usr/bin/python
import sc2reader
...
...
When I try to run it I get this:
(flaskapp)xxxx#xxxx-VirtualBox:~/flaskapp/bin$ ./test.py
Traceback (most recent call last):
File "./test.py", line 3, in <module>
import sc2reader
ImportError: No module named sc2reader
I've tried changing the shebang to reflect my VirtualEnv path for Python, but that didn't fix anything. The library is found in my site-packages folder in my virtual environment, so I'm not sure why I'm getting the ImportError.
I've never used VirtualEnv before so I'm assuming I configured it wrong so it's not seeing my site-packages.
Try using
#!/usr/bin/env python
as the shebang.
If that does not work, try seeing what is the output of which python.

Categories

Resources