How to import files in other Folder in Python - python-3.x

I'm using Python 3.10.
I have the following directory:
folder project
The File parse-subfinder.py , has a code:
parse-subfinder
But I get the error:
Traceback (most recent call last):
File "c:\Users\Windows\Desktop\smart-recon\subdomains\parse-subfinder.py", line 2, in
from ..utils.directory import Directory
ImportError: attempted relative import with no known parent package
How can I make this work?
I tried:
from ..utils.directory import Directory
from ..utils.discover import Discover

Related

ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) on python3.10

I Updated my python3 version to 3.10 now my sticky note giving this error
Traceback (most recent call last):
File "/usr/bin/indicator-stickynotes", line 21, in <module>
from stickynotes.gui import *
File "/usr/share/indicator-stickynotes/stickynotes/gui.py", line 20, in <module>
import gi
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)
I update my python from 3.8 to 3.9.5 and had the same issue. This was because python 3.10 is updated but the filename of the .so file of the package is not changed. So the __init__.py of the gi package will try to load a file whose name contains the previous version of python.
One solution would be make a copy of the .so file and rename it.
cd /usr/lib/python3/dist-packages/gi/
sudo cp _gi.cpython-{older version number}-x86_64-linux-gnu.so _gi.cpython-310-x86_64-linux-gnu.so
Please replace the {older version number} to a older python version so that the file of this name exists in the directory.

How to import from a module in the root directory?

I have the following project structure:
root
|--pkg1
| |--__init__.py
| |--other_stuff.py
|--stuff.py
In the real project, I also have a tests package in the root directory.
If I run unit tests with python -m unittest at root, it works seamlessly.
I want to make the other_stuff.py module also executable as a script.
However, if I run it from either root or pkg1 directories, the Python interpreter returns me this error:
Traceback (most recent call last):
File "pkg1/other_stuff.py", line 1, in <module>
from stuff import Dummy
ModuleNotFoundError: No module named 'stuff'
Here is the code:
# file pkg1/__init__.py
# (empty)
# file pkg1/other_stuff.py
from stuff import Dummy
if __name__ == '__main__':
# do other stuff
pass
# file stuff.py
class Dummy:
pass
I also tried the package relative imports from .stuff and from ..stuff, but it gives the error:
Traceback (most recent call last):
File "pkg1/other_stuff.py", line 1, in <module>
from ..stuff import Dummy
ImportError: attempted relative import with no known parent package
I'm not used to the Python3 import system.
What is the right way to import stuff.py in other_stuff.py to make it work both in the tests and as a script?
I have found a solution that might not be the best, but it worked.
To run other_stuff.py as a script, I just need to open the terminal in the root directory and run python3 -m pkg1.other_stuff.

ImportError: cannot import name 'cpyHook' from partially initialized module 'pyWinhook'

This has been plaguing me for a while now, and I'm not sure what to do about it. I've tried modifying the init.py pyWinhook script's imports, but to no avail. Here's the whole error:
Traceback (most recent call last):
File "d:\myuser\Documents\Python\evil_programs\keylogger.py", line 6, in <module>
import pyWinhook as pyHook
File "C:\Users\myuser\AppData\Roaming\Python\Python310\site-packages\pyWinhook\__init__.py", line 1, in <module>
from .HookManager import *
File "C:\Users\myuser\AppData\Roaming\Python\Python310\site-packages\pyWinhook\HookManager.py", line 1, in <module>
from . import cpyHook
ImportError: cannot import name 'cpyHook' from partially initialized module 'pyWinhook' (most likely due to a circular import) (C:\Users\myuser\AppData\Roaming\Python\Python310\site-packages\pyWinhook\__init__.py)
Thanks in advance
The way I fixed this is by installing Python 3.8.
Python 3.10 and 3.11 produced this issue.
I got this idea from the executable name on GitHub "pyWinhook-1.6.2.win-amd64-py3.8.exe"
Why it does not work with newer python, I have no clue.

ModuleNotFoundError: No module named 'scripts.trio_ircproxy'

I just started getting this error:
ImportError: attempted relative import with no known parent package
My directory structure us:
~/Documents/trio_ircproxy/scripts/trio_ircproxy/system_data.py
and I am running the interpreter from ~/Documents/trio_ircproxy/
(base) ashburry#ashburry-ThinkCentre-M92P:~/Documents/trio_ircproxy$ python3 trio_ircproxy.py
Traceback (most recent call last):
File "trio_ircproxy.py", line 35, in <module>
from scripts.trio_ircproxy.system_data import SystemData as system_data
ModuleNotFoundError: No module named 'scripts.trio_ircproxy'
SystemData is a class I am trying to import.
I was not getting this error before so I do not know what the problem is. Any Help?
I fixed this by putting an __init__.py file in the /scripts/ and /scripts/trio_ircproxy/ directories. The file is blank as I am not sure what to put in the __init__.py file.

ModuleNotFoundError: No module named lib

I have cloned a GitHub repository: https://github.com/xiaojunxu/SQLNet
I have followed the steps until downloading glove embeddings. On running python extract_vocab.py I get 'no module named lib error'.
The lib file is in the folder
The directory structure is as follows:
SQLNet
==>sqlnet
==>lib
==>__init__
==>dbengine
==>utils.py
==>extract_vocab.py
On executing python extract_vocab.py it gives me the following error:
(sql) C:\Users\khata\SQLNet>python extract_vocab.py
Traceback (most recent call last):
File "extract_vocab.py", line 3, in <module>
from sqlnet.utils import *
File "C:\Users\khata\SQLNet\sqlnet\utils.py", line 2, in <module>
from lib.dbengine import DBEngine
ModuleNotFoundError: No module named 'lib'
lib directory is present with dbengine in it.
I am working in WINDOWS with a python3 environment
I copied and pasted the contents of the dbengine file in utils.py.Which solved the problem
Change it to from .lib.dbengine import DBEngine

Resources