If Python imports a file from your project instead of the package you installed, first identify the file Python is loading. On macOS, check the active interpreter, the imported module’s path, and any project files or folders with the same name. Rename the conflicting local file, remove its stale bytecode cache if needed, and run the project with the intended Python environment.

1. Check which Python and module are in use

In Terminal, go to the project folder and ask Python to report the module’s origin. Replace requests with the import name that is failing; for example, a package installed as Pillow is imported as PIL.

cd "/path/to/your/project"
command -v python3
python3 -c 'import sys, importlib.util; name = "requests"; print("Python:", sys.executable); print("Module:", importlib.util.find_spec(name))'

A result with an origin under your project directory, such as .../requests.py, points to a local shadowing file. Python puts the script’s directory or current working directory early in its search path, so a same-named local module can take precedence over an installed package. Python’s module search path documentation

If the command reports None, that interpreter cannot find the module by that name. If it shows a location in site-packages, the collision may be elsewhere: your app may launch a different interpreter, the import name may differ from the package’s installation name, or the error may arise inside a package dependency.

2. Find and rename the local conflict

Look in the project for a file named after the import, such as requests.py, or a directory with that name. Also check for common standard-library names such as email.py, json.py, or typing.py. Rename the file or directory to a project-specific name, then update any imports that referred to that local module. Avoid merely renaming it to another common package or standard-library name.

If the conflicting file is an intentional project module, keep it but give it a distinctive name and import it through your project’s package structure. A project file that happens to share a third-party package’s name is especially likely to cause confusing partial imports or errors about missing attributes.

After renaming, remove only the matching __pycache__ directory if one remains beside the old file. Python normally checks source-file modification times and refreshes bytecode caches automatically; deleting the matching cache is a cleanup step, not a substitute for resolving the name conflict. Python’s module and bytecode documentation

3. Make sure the package belongs to the Python you run

On macOS, it is common to have more than one Python installation or project environment. The executable shown as sys.executable is the interpreter behind the command you just ran. Install packages through that interpreter so pip and Python do not accidentally point to different environments:

python3 -m pip show requests
python3 -m pip install requests

Use the package’s distribution name with pip show and its import name with Python. They can differ; for example, Pillow is commonly imported as PIL. A “package not found” result from pip show means that distribution is not installed for the interpreter used by that command.

For a project-specific environment, create and activate a virtual environment from the project directory, then install the dependency there:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install requests
python -c 'import requests; print(requests.__file__)'

With the environment active, python should resolve to its interpreter. The final command prints the actual loaded file path; check that it is under .venv rather than your project’s top level. Virtual environments isolate project packages from other environments and are created with python -m venv. Python’s venv documentation

4. Check path overrides and rerun cleanly

If the module still resolves to an unexpected location, inspect Python’s search path and any PYTHONPATH override:

python -c 'import os, sys; print("PYTHONPATH:", os.environ.get("PYTHONPATH")); print("sys.path:"); print("\n".join(sys.path))'

sys.path is initialized using PYTHONPATH and installation-dependent defaults; in common invocations, Python also prepends the script directory or current working directory. Remove an unintended PYTHONPATH setting from the current shell with unset PYTHONPATH, then rerun the diagnostic. For persistent settings, inspect the shell startup file where it was set and remove or correct that export. Python’s sys.path documentation

Restart the Python process after renaming files or changing environments: an already-running interpreter can retain modules it has imported in memory. Then repeat the origin check and run the command or application the same way you normally do. If the origin is correct but the import still fails, read the full traceback; it may identify a missing dependency or an error raised while the package loads, rather than a name collision.

Sources