Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import snscrape as library when installed using pip. #1049

Closed
fthbaskin opened this issue Jul 11, 2024 · 3 comments
Closed

Cannot import snscrape as library when installed using pip. #1049

fthbaskin opened this issue Jul 11, 2024 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@fthbaskin
Copy link

Describe the bug

I have installed snscrape into my venv using pip. The current version is snscrape-0.7.0.20230622 and I am using Python 3.12.4 with pip 24.1.2 version. I use Windows 10 as OS and Powershell as a console.

The problem is when I try to import the module using import snscrape.modules.instagram as snIns I get the following exception. Also, I get a similar exception when I try to use CLI.

  File "try.py", line 10, in <module>
    import snscrape.modules.instagram as snIns
  File ".venv\Lib\site-packages\snscrape\modules\__init__.py", line 17, in <module>
    _import_modules()
  File ".venv\Lib\site-packages\snscrape\modules\__init__.py", line 13, in _import_modules
    module = importer.find_module(moduleName).load_module(moduleName)

It seems like a bizarre bug. I would like to help you to fix this bug.

How to reproduce

The problem is when I try to import the module using import snscrape.modules.instagram as snIns I get the following exception. Also, I get a similar exception when I try to use CLI.

  File "try.py", line 10, in <module>
    import snscrape.modules.instagram as snIns
  File ".venv\Lib\site-packages\snscrape\modules\__init__.py", line 17, in <module>
    _import_modules()
  File ".venv\Lib\site-packages\snscrape\modules\__init__.py", line 13, in _import_modules
    module = importer.find_module(moduleName).load_module(moduleName)

Expected behaviour

import snscrape.modules.instagram as snIns should work correctly and I should be able to use the library.

Screenshots and recordings

No response

Operating system

Windows 10 Enterprise 2009

Python version: output of python3 --version

3.12.4

snscrape version: output of snscrape --version

0.7.0.20230622

Scraper

Instagram

How are you using snscrape?

Module (import snscrape.modules.something in Python code)

Backtrace

  File "try.py", line 10, in <module>
    import snscrape.modules.instagram as snIns
  File ".venv\Lib\site-packages\snscrape\modules\__init__.py", line 17, in <module>
    _import_modules()
  File ".venv\Lib\site-packages\snscrape\modules\__init__.py", line 13, in _import_modules
    module = importer.find_module(moduleName).load_module(moduleName)

Log output

No response

Dump of locals

No response

Additional context

No response

@fthbaskin fthbaskin added the bug Something isn't working label Jul 11, 2024
@fthbaskin fthbaskin changed the title Cannot import snscrape as library. Cannot import snscrape as library when installed using pip. Jul 11, 2024
@fthbaskin
Copy link
Author

fthbaskin commented Jul 11, 2024

Apparently, commenting out everything in modules/__init__.py solved the issue.

@TheTechRobo
Copy link
Contributor

#782

@CortoRikasa
Copy link

Encountered the same problem. Changed modules/init.py to the following:

import pkgutil
import importlib

all = []

def _import_modules():
prefixLen = len(name) + 1
for importer, moduleName, isPkg in pkgutil.iter_modules(path, prefix = f'{name}.'):
assert not isPkg
moduleNameWithoutPrefix = moduleName[prefixLen:]
all.append(moduleNameWithoutPrefix)
module = importlib.import_module(moduleName)
globals()[moduleNameWithoutPrefix] = module

_import_modules()

@JustAnotherArchivist JustAnotherArchivist added duplicate This issue or pull request already exists and removed bug Something isn't working labels Dec 17, 2024
@JustAnotherArchivist JustAnotherArchivist closed this as not planned Won't fix, can't repro, duplicate, stale Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

4 participants