Possible import name clash between project directory and installed *.py executables
See original GitHub issuePython’s import system has one weird aspect that make it vulnerable to import name clashes between modules in the working directory and in the directory of the main entry point. This affects Scrapy’s import mechanism.
Minimal example:
touch $(dirname `which scrapy`)/myproject.py # Could be some file from another program
scrapy startproject myproject
cd myproject
scrapy genspider example example.com
scrapy crawl example
… fails with: ModuleNotFoundError: No module named 'myproject.settings'; 'myproject' is not a package
Possible solution
Just remove the problematic folder from sys.path
, i.e. add sys.path.remove(os.path.dirname(__file__))
to the Scrapy executable (usually /usr/bin/scrapy
or /usr/local/bin/scrapy
).
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Python error "ImportError: No module named" - Stack Overflow
To check this, just do which python , and see if the executable is the one that is in your local directory. If...
Read more >Python import: Advanced Techniques and Tips
In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring...
Read more >how the Python import system works - Ten thousand meters
When Python imports such a directory, it executes the __init__.py file, so the names defined there become the attributes of the module. The...
Read more >2. Writing the Setup Script — Python 3.11.1 documentation
The main purpose of the setup script is to describe your module distribution to... ... The values are directory names relative to your...
Read more >Issues When Using auto-py-to-exe - Nitratine
--name: The name of the output folder/executable; --hidden-import: If the executable says a module is missing, make sure you have it installed ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I had forgotten about it, but I think #3669 may be tackling this issue (on the documentation level).
Can you please explain how is it a bug in Scrapy specifically and not just a common Python problem with having modules with the same name in different sys.path components?
Sure, it’s a common part of solutions to path-specific problems, but there is no single thing you can or should do to prevent conflicts. What you are proposing can only solve some specific problems you have and not the general one.