question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible import name clash between project directory and installed *.py executables

See original GitHub issue

Python’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:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Gallaeciocommented, Feb 6, 2022

I had forgotten about it, but I think #3669 may be tackling this issue (on the documentation level).

0reactions
wRARcommented, Feb 1, 2022

I definitively see it as a bug in Scrapy when a specific name of the project folder can result in a crash.

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?

I don’t see an argument against modifying sys.path in general ­- it is the common method to adjust imports paths.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found