MacOS下Python3.6无法识别自建包模块,路径添加失败求助
Hey there! Let’s work through why your custom package isn’t being recognized in Python 3.6 on macOS—this is a super common gotcha, and I’ve sorted through it plenty of times. Let’s break down the possible issues step by step:
1. First: Confirm You’re Actually Using Python 3.6
MacOS comes with Python 2 pre-installed, so it’s easy to mix up versions without realizing it. Open your terminal and run these commands to double-check:
which python3 python3 --version
Make sure the output points to your Python 3.6 installation (e.g., /Library/Frameworks/Python.framework/Versions/3.6/bin/python3). If it’s pointing to a different version, you might need to use python3.6 explicitly instead of python3.
2. Test Temporary Path Addition First
Before troubleshooting permanent setup, let’s verify that adding the path manually works. Open a Python 3.6 shell and run:
import sys # Replace with your package's absolute path sys.path.append("/Users/YourUsername/Projects/your_custom_package") print(sys.path)
Look for your package path in the printed list. If it’s there, try importing your module now—if it works, the problem is with your permanent path setup, not the path itself. If it still doesn’t work:
- Double-check the path is absolute (no relative paths like
../your_package). - Make sure your package directory has an
__init__.pyfile (even an empty one works for Python 3.6 to recognize it as a package).
3. Fix Permanent Path Setup
If temporary addition works but permanent doesn’t, here are the most common mistakes:
a. You’re Editing the Wrong Shell Config File
- If you use the default Terminal app with bash: Edit
~/.bash_profile - If you use zsh (default on newer macOS versions): Edit
~/.zshrc
b. Your Export Command Is Incorrect
Add this line to your correct config file, replacing the path with your package’s absolute path:
export PYTHONPATH="/path/to/your/package:$PYTHONPATH"
The :$PYTHONPATH at the end ensures you don’t overwrite existing Python paths—you’re just adding your new one to the list.
c. You Didn’t Reload the Config File
After editing the file, either:
- Close and re-open your terminal, or
- Run this command to reload it immediately:
# For bash source ~/.bash_profile # For zsh source ~/.zshrc
Then verify the path is set by running:
echo $PYTHONPATH
You should see your package path in the output.
4. Alternative: Use a .pth File
If the PYTHONPATH method isn’t working, try this more reliable approach for Python-specific paths:
- Open a Python 3.6 shell and run:
This will show you the site-packages directories for Python 3.6 (look for one likeimport site print(site.getsitepackages())/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages). - Navigate to that directory in your terminal.
- Create a new file named
my_package.pth(the name doesn’t matter, only the.pthsuffix). - Open the file and paste your package’s absolute path (one path per line if you have multiple).
- Save the file. Now every time you start Python 3.6, it will automatically add this path to
sys.path.
Quick Package Structure Check
Just to be thorough, make sure your package follows this basic structure:
your_custom_package/ __init__.py # Required for Python 3.6 to recognize this as a package your_module.py # The module you want to import
Give these steps a try, and you should have your package recognized in no time!
内容的提问来源于stack exchange,提问作者Nice Person

