By Dalya Gartzman
You are here because you are using:
- Windows OS version 10+
- Python version 3.3+
- Anaconda3
And you would like to edit your PYTHONPATH permanently.
TL;DR
- Go to
C:\Users\<your_username>\Anaconda3\Lib\site-packages - Create a file
python37.pth - Edit the file to include this line
C:\\Users\\<your_username>\\my_module
The Long version; Do Read
Prologue
In most cases, editing the PYTHONPATH from the Settings GUI will do the trick. The trick is well explained in this Stack Overflow answer.
If in the first place you are only looking to edit your path locally, this helpful answer will do.
Slightly Extended Item no. 1
If you don’t have C:\Users\<your_username>\Anaconda3\Lib\site-packages, replace C:\Users\<your_username> with the path to your Anaconda3.
Slightly Extended Item no. 2
If you are using Python3.7, create a file called python37.pth. Else create a file called python<XX>.pth for whatever Python version you are using.
- Not sure which version?
UnderC:\Users\<your_username>\Anaconda3\ search for a file of theform python<XX>.dll. The indicates the version number you need for naming your .pth file. - Windows is super annoying and won’t let you create a file with a
.pthsuffix?
There are such files in theC:\Users\<your_username>\Anaconda3\Lib\site-packages folder. Copy one of them and edit the prefix. - Some places say you need to create a
._pthfile instead of.pth?
A._pthfile will completely replace your existing path. While a.pthfile will append its content to the path you already have. You can find more information here.
Slightly Extended Item no. 3
Assuming the SuperCoolClass you wish to import is located atC:\Users\<your_username>\my_project_folder\my_awesome_file.py .
Then open your newly created python<XX>.pth file with your favorite text editor (please don’t say it’s Vim) and add one line:C:\\Users\\<your_username>\\my_project_folder.
Yes, with those annoying double slashes \ .
No, without quotes "" .
And that’s it.
Now you can import from anywhere, like a normal person:from my_awesome_file import SuperCoolClass .
Epilogue
Nothing important to add here really.
I just hope my 2 hours of frustration + 1 hour of writing this post saved you some time.
Peace out.