Run Python program as administrator from command line - python

I'm trying to run my program as an administrator from the command line. Seems quite easy but I'm struggling.
I started with this, which works fine launched from the Python IDLE:
import dropbox, pymysql, shlex, shutil, subprocess
import logging
import sshtunnel
from sshtunnel import SSHTunnelForwarder
# Main program starts here
print("Hello World!")
but that gave an error as follows:
pi#jsmd:~ $ sudo python /home/pi/Documents/iot_pm2/iot.py
Traceback (most recent call last):
File "/home/pi/Documents/iot_pm2/iot.py", line 3, in <module>
import dropbox, pymysql, shlex, shutil, subprocess
ImportError: No module named pymysql
After some Googling I added two lines at the start:
import sys
sys.path.append("/usr/local/lib/python3.4/dist-packages/")
This also works fine from the Python IDLE but gives a long error trace when run from the command line... and I'm not good enough at understanding how to read the trace and hence what I can do to fix it. (Plus I don't understand why it works from the IDLE as a non-admin but not from the command line as an admin.) Can anyone help?
pi#jsmd:~ $ sudo python /home/pi/Documents/iot_pm2/iot.py
Traceback (most recent call last):
File "/home/pi/Documents/iot_pm2/iot.py", line 5, in <module>
import sshtunnel
File "/usr/local/lib/python3.4/dist-packages/sshtunnel.py", line 25, in <module>
import paramiko
File "/usr/local/lib/python3.4/dist-packages/paramiko/__init__.py", line 30, in <module>
from paramiko.transport import SecurityOptions, Transport
File "/usr/local/lib/python3.4/dist-packages/paramiko/transport.py", line 50, in <module>
from paramiko.dsskey import DSSKey
File "/usr/local/lib/python3.4/dist-packages/paramiko/dsskey.py", line 26, in <module>
from Crypto.PublicKey import DSA
File "/usr/local/lib/python3.4/dist-packages/Crypto/PublicKey/DSA.py", line 89, in <module>
from Crypto import Random
File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/__init__.py", line 29, in <module>
from Crypto.Random import _UserFriendlyRNG
File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 38, in <module>
from Crypto.Random.Fortuna import FortunaAccumulator
File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/Fortuna/FortunaAccumulator.py", line 39, in <module>
from . import FortunaGenerator
File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/Fortuna/FortunaGenerator.py", line 35, in <module>
from Crypto.Util import Counter
File "/usr/local/lib/python3.4/dist-packages/Crypto/Util/Counter.py", line 59, in <module>
from Crypto.Util import _counter
ImportError: cannot import name _counter
Thanks.

is the default of python is 3 ? If not try to execute like this: sudo
python3 ... – #Koffee
Thanks to #Koffee we have an answer and this thread can be marked as solved and closed.

Related

i've been making a discord bot on replit. it's been working perfectly then today it broke

my bot has been running perfectly on replit for 2 days now. I used replit servers and uptimerobot to keep it up and running and it worked flawlessly until suddenly the bot goes offline.
I check the code and it has stopped working all it gives me is this error code:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import discord
File "/home/runner/discord-bot/venv/lib/python3.8/site-packages/discord/__init__.py", line 25, in <module>
from .client import Client
File "/home/runner/discord-bot/venv/lib/python3.8/site-packages/discord/client.py", line 33, in <module>
import aiohttp
File "/home/runner/discord-bot/venv/lib/python3.8/site-packages/aiohttp/__init__.py", line 6, in <module>
from .client import (
File "/home/runner/discord-bot/venv/lib/python3.8/site-packages/aiohttp/client.py", line 35, in <module>
from . import hdrs, http, payload
File "/home/runner/discord-bot/venv/lib/python3.8/site-packages/aiohttp/http.py", line 7, in <module>
from .http_parser import (
File "/home/runner/discord-bot/venv/lib/python3.8/site-packages/aiohttp/http_parser.py", line 15, in <module>
from .helpers import NO_EXTENSIONS, BaseTimerContext
File "/home/runner/discord-bot/venv/lib/python3.8/site-packages/aiohttp/helpers.py", line 48, in <module>
from typing_extensions import Protocol
ModuleNotFoundError: No module named 'typing_extensions'
Personally I use replit as well this seems like a package error, try deleting the package configuration files found on your repl and try re running the program so it'll re install your packages. If this doesn't work try running pip install typing_extensions in the shell.

ImportError: cannot import name 'winrandom'

I know that this question has already a ton of answers but unfortunately this hasn't fixed the issue for me. I want to run some code using PyCrytpo but winrandom doesn't want to get import.
All the other posts are for earlier releases of python. I am using Python 3.6.5 and renaming code within nt.py file to "from . import winrandom" doesn't do the trick for me.
Do you have any suggestions or is a rollback the only opportunity?
Windows 10
Python 3.6.5
Anaconda
from Crypto.PublicKey import RSA
Traceback (most recent call last):
File "<ipython-input-39-b6fe4d249eef>", line 1, in <module>
from Crypto.PublicKey import RSA
File "C:\Users\Markus\Anaconda3\lib\site-packages\Crypto\PublicKey\RSA.py", line 78, in <module>
from Crypto import Random
File "C:\Users\Markus\Anaconda3\lib\site-packages\Crypto\Random\__init__.py", line 28, in <module>
from Crypto.Random import OSRNG
File "C:\Users\Markus\Anaconda3\lib\site-packages\Crypto\Random\OSRNG\__init__.py", line 34, in <module>
from Crypto.Random.OSRNG.nt import new
File "C:\Users\Markus\Anaconda3\lib\site-packages\Crypto\Random\OSRNG\nt.py", line 28, in <module>
from . import winrandom
ImportError: cannot import name 'winrandom'

Can only run my copy_files.py from one directory

I can run my copy_files.py script from only one directory.
When I run it from other directories, I get the following errors:
Traceback (most recent call last):
File "./copy_py", line 36, in <module>
import paramiko
File "/usr/local/lib/python3.6/site-packages/paramiko/__init__.py", line 22, in <module>
from paramiko.transport import SecurityOptions, Transport
File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 56, in <module>
from paramiko.dsskey import DSSKey
File "/usr/local/lib/python3.6/site-packages/paramiko/dsskey.py", line 27, in <module>
from cryptography.hazmat.primitives.asymmetric.utils import (
File "/usr/local/lib/python3.6/site-packages/cryptography/hazmat/primitives/asymmetric/utils.py", line 9, in <module>
from asn1crypto.algos import DSASignature
File "/usr/local/lib/python3.6/site-packages/asn1crypto/algos.py", line 26, in <module>
from .core import (
File "/usr/local/lib/python3.6/site-packages/asn1crypto/core.py", line 53, in <module>
import copy
File "/home/xjacrawf/work/pyth/copy.py", line 106, in <module>
cli = paramiko.client.SSHClient()
AttributeError: module 'paramiko' has no attribute 'client'
I have tried to remove all of the script's body, even renamed the script.
#!/usr/local/bin/python3.6
import sys
sys.path.insert(0, "/usr/local/lib/python3.6/site-packages")
import cx_Oracle
import glob
import os
import time
import paramiko
from scp import SCPClient
cli = paramiko.client.SSHClient()
cli.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
I expect the script to establish a connection, and it just errors.
I run the script from the first directory it ran from $MDS_SRC/ and it runs fine.
I can copy the entire script to my local directory , and it errors out.

'import discord' not working in Python 3.5

I try to import discord in python 3.5, but I get the following traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python\Python35-32\lib\site-packages\discord\__init__.py", line 20, in <module>
from .client import Client, AppInfo, ChannelPermissions
File "C:\Python\Python35-32\lib\site- packages\discord\client.py", line 42, in <module>
from .voice_client import VoiceClient
File "C:\Python\Python35-32\lib\site- packages\discord\voice_client.py", line 65, in <module>
from .gateway import *
File "C:\Python\Python35-32\lib\site- packages\discord\gateway.py", line 31, in <module>
import aiohttp
File "C:\Python\Python35-32\lib\site- packages\aiohttp\__init__.py", line 10, in <module>
from .protocol import * # noqa
File "C:\Python\Python35-32\lib\site- packages\aiohttp\protocol.py", line 11, in <module>
from wsgiref.handlers import format_date_time
ImportError: cannot import name 'format_date_time'
I installed discord inside Python35-32\Scripts using pip install discord. Additionally, I installed asyncio using pip install asyncio in the same directory.
The error pops up in my command prompt, when I type in "python", wait for it to open, and then type "import discord". I'm saying that just to be clear that it's not a "I have a file named XXX.py which clashes with something from the discord library" type of error.
Based on the comments you have added and the line
File "C:\Python\Python35-32\lib\site- packages\aiohttp\protocol.py", line 11, in <module>
from wsgiref.handlers import format_date_time
ImportError: cannot import name 'format_date_time'
The problem seems to be that 'format_date_time' could not be imported from the built in library wsgiref. Turns out that the file ...\Python35-32\lib\wsgiref\handlers.py was completely empty indicating that something corrupted your python installation.
Two possible solutions:
Reinstall your python completely which also has the benefit of solving other potential corruptions that you might not have noticed yet (Note that the underlying cause for this corruption remains unknown)
Go into the Cython git and copy the contents of the handlers.py into the local file. This is a very specific solution however and you should be aware that other parts of your python installation might also be corrupt.
Maybe you wanted to pip install discord.py instead of discord?

Python, nltk: Cannot import mkdtemp

I got this error while trying to compile test.py that contains these two commands -
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
This is the error I got
Traceback (most recent call last):
File "test.py", line 3, in <module>
import nltk
File "C:\Anaconda2\lib\site-packages\nltk\__init__.py", line 89, in <module>
from nltk.internals import config_java
File "C:\Anaconda2\lib\site-packages\nltk\internals.py", line 29, in <module>
from nltk import compat
File "C:\Anaconda2\lib\site-packages\nltk\compat.py", line 206, in <module>
from tempfile import mkdtemp
ImportError: cannot import name mkdtemp
The strange thing is, that these same two commands when executed on the command line work perfectly fine.
What could be the reason for this to happen? And more importantly, how do I fix this?
EDIT:
This is what i got when i executed print(sys.path)
['', 'C:\\Anaconda2\\python27.zip', 'C:\\Anaconda2\\DLLs', 'C:\\Anaconda2\\lib', 'C:\\Anaconda2\\lib\\plat-win', 'C:\\Anaconda2\\lib\\lib-tk', 'C:\\Anaconda2', 'C:\\Anaconda2\\lib\\site-packages', 'C:\\Anaconda2\\lib\\site-packages\\Sphinx-1.3.5-py2.7.egg', 'C:\\Anaconda2\\lib\\site-packages\\win32', 'C:\\Anaconda2\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda2\\lib\\site-packages\\Pythonwin', 'C:\\Anaconda2\\lib\\site-packages\\setuptools-20.3-py2.7.egg']
EDIT2:
I realised that I had a file called tempfile.py in my working directory. Upon deletion everything works fine
Make sure you don't have tempfile.py somewhere in python path; which prevents import of standard library tempfile module.
Also make sure deleting tempfile.pyc if there it is.

Categories