Error occuring when importing module "threading" - python

In a project I need to import RLock from threading module in Python3.6
But no matter what I do, I still have the same error :
from threading import RLock
ImportError: cannot import name 'RLock'
Each time I want to import this module (threading) I have this error.
I already upgraded pip, but it doesn't work.
If someone has a solution for me I would be very grateful.
Edit
This error doesn't depend on the folder in which I am located. I notice that when I am running the command in Python2.7 :
import threading
It works.
But if I am running this command under Python3.6 it doesn't work, still with the same output error.
I checked and the only threading.py files I have are :
/snap/core/6673/usr/lib/python3.5/threading.py
/snap/core/6818/usr/lib/python3.5/threading.py
/snap/core/6964/usr/lib/python3.5/threading.py
/snap/core18/941/usr/lib/python3.6/threading.py
/snap/core18/970/usr/lib/python3.6/threading.py
/snap/docker/384/usr/lib/python2.7/threading.py
/snap/docker/384/usr/lib/python3.5/threading.py
/snap/gnome-3-26-1604/74/usr/lib/python3.5/threading.py
/snap/gnome-3-26-1604/82/usr/lib/python3.5/threading.py
/snap/gnome-3-28-1804/36/usr/lib/python3.6/threading.py
/snap/gnome-3-28-1804/40/usr/lib/python3.6/threading.py
/snap/libxml2/69/usr/lib/python2.7/threading.py
/usr/lib/python2.7/threading.py
/usr/lib/python2.7/threading.pyc
/usr/lib/python3.6/threading.py
/usr/lib/python3.7/threading.py

I think you have a Python file which called threading. You should rename your file and the import will work. I have just tried it with Python3.6.6 and it works as expected.
Code
from threading import RLock
rl = RLock()
print(rl)
Output:
>>>python other.py
<_RLock owner=None count=0>

I found the origin of the problem by looking at the complete error logs (which I should have specified here, I apologize). Turns out I have a file named token.py. By renaming it the error disappears.
Thank you for your answers.

Related

ImportError: cannot import name 'EXTRACTOR_FILENAME' from 'talon.signature'

I am having trouble getting an import statement to work. I am attempting to use this package:
https://github.com/mailgun/talon
I am running the following command:
from talon.signature import EXTRACTOR_FILENAME, EXTRACTOR_DATA
I get the following error:
ImportError: cannot import name 'EXTRACTOR_FILENAME' from 'talon.signature' (system path to file)
While troubleshooting I don't see EXTRACTOR_FILENAME or EXTRACTOR_DATA defined anywhere. I did a search in directory for all files. Is there some sort of convention in python where EXTRACTOR_FILENAME maps to a specific class?
UPDATE: Figured it out, just something as simple as manually defining the 2 constants. The docs weren't exactly clear or I missed it.
For your project the import looks like this:
import talon
from talon import quotations
Put those statements on the top of your file, and it should work.
if you don't have the packages on your system type this in your terminal:
pip install talon
The Github repo also explains this

How to test python code which imports libraries available only in production server

I'm trying to test a python module which is part of a windows service setup in a production server. However, the module I'm trying to test imports other python modules which, in turn, import libraries that only exist in a production server. Example below:
ModuleAlpha.py:
import A
import B
class X(object):
pass
A.py:
import _winreg as reg
import pymqi
import CMQC
class Y(object):
pass
MyTestModule.py:
from ModuleAlpha import X
I receive the following error when I try to run MyTestModule.py:
ImportError: No module named pymqi
I understand why I get the import error. My question is: is there a way to circumvent the module import that creates my problem?
Worth to mention that commenting out the use of pymqi in module A is not possible due to how the code is written.
So - there's two answers to this. The right answer and the wrong answer.
The right answer, is to sell your company on the benefits of having a pre-prod environment as similar as possible to prod. Having libraries in use in prod that aren't available in pre-prod is.... sub-optimal ("sub-optimal" is my way of being polite. The honest word is significantly less polite).
The wrong answer is to catch the ImportError exception. There are a couple of approaches you could take here.
try:
import pymqi
HAVE_PYMQI = True
except ImportError:
HAVE_PYMQI = False
if HAVE_PYMQI:
blah()
OR
try:
import pymqi
except ImportError:
import fakepymqi as pymqi
Obviously for the latter wrong solution, you'd need to implement fakepymqi yourself.

how to import modules from local repository with virtualenv and pip

I have a question that I assume has a simple answer, but for some reason I am struggling to find it on my own. I have created and activated a virtual environment with virtualenv, and I am trying to install all the necessary packages in order to create a requirements.txt file.
I have, for example, a Python file that begins like this:
import xml.etree.ElementTree as ET
from lib.project import Projector
from lib import writer
import os
import datetime
from datetime import timedelta
from datetime import datetime
import pprint
When I try to run this file from the virtual machine, I receive the following error:
Traceback (most recent call last):
File "readMap.py", line 2, in <module>
from lib.project import Projector
ModuleNotFoundError: No module named 'lib.project'
My problem is that I'm not sure why the virtual environment can't find project.py. My directory structure is:
regiaoSul
lib
__init__.py
arrival_conversion.py
coord_conversion.py
message_conversion.py
project.py
route_conversion.py
stop_conversion.py
wkt_parser.py
writer.py
readMap.py
json_generator.py
The import on line 2 implies lib is a module rather than "a simple repository".
I will try running the script with the flag -m. Something like this -
python -m script_name
make sure to drop the .py extension when you run with -m flag.
Another advice: you don't need to install python files to the virtual environment, they are not some external libraries. They only need to be present (with the same order of packaging) when you run your script.
Thanks to everyone who responded. I believe the issue was some sort of dependency problem. In readMap.py I had imported writer from lib, and in writer.py I had imported Projector from project. I moved the function that required Projector from writer.py to readMap.py and it worked.
I still don't fully understand why this was a problem. Until recently I had been running my scripts in PyCharm and they all worked with the structure I had. It was only when I tried to run them from the command line in my virtual machine that they didn't work.
If anybody would like to explain the distinction to me and what the exact problem was with my imports, feel free to.
I sometimes face the same issue. A solution is to add the path to sys.path by:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")

Layer [<class 'scapy.layers.snmp.SNMP'>] not found

I'm trying to bruteforce (a rootme challenge don't worry :p) an SNMPv3 authentication password using this python script : https://github.com/cysboy/SnmpCrack/blob/master/SnmpCrack.py
Nevertheless, I have an error loading a scapy snmp module line 72 on this code
snmp = pkt[SNMP]
I printed the error by adding
except Exception as e :
print("Continuing")
print(e)
continue
In the followed try catch.
I get this error : Layer [<class 'scapy.layers.snmp.SNMP'>] not found
So I tried to search where this class should have been declared.
I found a file under /usr/lib/python2.7/dist-packages/scapy/layers/snmp.py where the class is defined.
I tried to import this file manually and other modules with
import sys
sys.path.insert(0, "/usr/lib/python2.7/dist-packages/scapy/layers/")
import snmp
from scapy.all import *
from scapy.layers import *
from scapy.layers.snmp import * #SNMP, SNMPresponse, SNMPvarbind
But I don't really know what I'm doing exactly.
Is there a way to be sure this file is included as a scapy submodule / class ?
I'm running under the latest Kali 64 bit. I tried with python3 aswell without success.
I'm running this script with python 2.7.14+
Thanks if you can help me with that :)
Have a good day
Quite old question already.
As you can see in the code source,
https://github.com/secdev/scapy/blob/master/scapy/layers/snmp.py
the fields do exist in recent versions.
Try uninstalling scapy from apt, and install it via github or via
pip install scapy
It may be a problem of versioning...

cx_freeze no module named 'pkg_resources'

I'm using Python 3.3.5 with cx-freeze 4.3.3 on windows 8.1.
I'm trying to cx_freeze a program that uses pkg_resources.
I initially had it in my setup file under packages, but when I tried to freeze it the processes stopped with an error Import Error: No module named 'pkg_resources'.
I then moved it in the setup file from packages to includes. The cx_freeze process completed this time but when I tried to start the application I got another error message.
If I go to my IDE and try to import pkg_resources it works fine.
>>> import pkg_resources
>>> pkg_resources
<module 'pkg_resources' from 'C:\\Python33\\lib\\site-packages\\setuptools-18.0.1-py3.3.egg\\pkg_resources\\__init__.py'>
There's a similar question asked here, and the solution is to re-install setuptools. So I downloaded setuptools 18.0.1 and installed it via cmd, but it did not solve my problem and I'm still getting the same errors with cx_freeze.
Any help getting this to work would be greatly appreciated.
Edit: My solution (hack) has been to write the dependency out of yagmail. Yagmail's original _innit__.py...
from pkg_resources import get_distribution
__project__ = 'yagmail'
__version__ = get_distribution(__project__).version
I first put a print statement in there to get the version, and then hard coded it.
__project__ = 'yagmail'
__version__ = '0.4.84'
Though this has solved my problem it isn't really the answer, so will leave this open should someone have a solution that keeps pkg_resources.

Categories

Resources