pythonpath在哪
You are here because you are using:
您在这里是因为您正在使用:
- Windows OS version 10+ Windows OS版本10+
- Python version 3.3+ Python版本3.3+
- Anaconda3 Python3
And you would like to edit your PYTHONPATH
permanently.
您想永久编辑您的PYTHONPATH
。
TL; DR (TL;DR)
Go to
C:\Users\<your_username>\Anaconda3\Lib\site-pa
ckages转到
C:\Users\<your_username>\Anaconda3\Lib\site-pa
cksCreate a file
python37.pth
创建一个文件
python37.pth
Edit the file to include this line
C:\\Users\\<your_username>\\my_
module编辑文件以包括此行
C:\\Users\\<your_username>\\my_
模块
加长版; 阅读 (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.
在大多数情况下,从设置GUI编辑PYTHONPATH
。 这个技巧在Stack Overflow答案中得到了很好的解释。如果首先,您只是想在本地编辑路径,那么这个有用的答案就可以了。
略有扩展 1个 (Slightly Extended Item no. 1)
If you don’t have C:\Users\<your_username>\Anaconda3\Lib\site-pa
ckages, replace C:\Users\<your_
username> with the path to your Anaconda3.
如果您没有C:\Users\<your_username>\Anaconda3\Lib\site-pa
您的C:\Users\<your_username>\Anaconda3\Lib\site-pa
place C:\Users\<your_
,请使用Anaconda3的路径place C:\Users\<your_
用户名>。
略有扩展 2 (Slightly Extended Item no. 2)
If you are using Python3.7, create a file called python37.pth
. Else create a file called python<XX&g
t;.pth for whatever Python version you are using.
如果您使用的是Python3.7,请创建一个名为python37.pth
的文件。 否则,为您使用的任何Python版本创建一个名为python<XX&g
t; .pth的文件。
Not sure which version?
不确定哪个版本?
Under
下
C:\Users\<your_username>\Anac
onda3\ search for a file of theform python&l
t;XX>.d
ll. The <XX> indicates the version number you need fo
r naming your .pth file.C:\Users\<your_username>\Anac
onda3 \搜索form python&l
t; XX&gt;.d
ll的文件。 该<XX>表示您东东版本号d fo
[R命名的.pth文件。Windows is super annoying and won’t let you create a file with a
.pth
suffix?Windows非常烦人,不会让您创建带有
.pth
后缀的文件吗?There are such files in the
在这些文件中
C:\Users\<your_username>\Anaconda3\Lib\site-pa
ckages folder. Copy one of them and edit the prefix.C:\Users\<your_username>\Anaconda3\Lib\site-pa
cks文件夹。 复制其中之一并编辑前缀。Some places say you need to create a
._pth
file instead of.pth
?有些地方说,你需要创建一个
._pth
文件,而不是.pth
?A
一个
._pth
file will completely replace your existing path. While a.pth
file will append its content to the path you already have. You can find more information here.._pth
文件将完全替换您现有的路径。.pth
文件会将其内容附加到您已有的路径中。 您可以在此处找到更多信息。
略有扩展 3 (Slightly Extended Item no. 3)
Assuming the SuperCoolClass
you wish to import is located at C:\Users\<your_username>\my_project_folder\my_awesome_f
ile.py .
假设您要导入的SuperCoolClass
位于C:\Users\<your_username>\my_project_folder\my_awesome_f
my_awesome_file.py。
Then open your newly created python<XX&g
t;.pth file with your favorite text editor (please don’t say it’s Vim) and add one line:
C:\\Users\\<your_username>\\my_pr
oject_folder.Yes, with those annoying doubl
e slashes \\ .No, witho
ut quotes "" .
然后使用您喜欢的文本编辑器(请不要说是Vim)打开新创建的python<XX&g
t; .pth文件并添加line:
C:\\Users\\<your_username>\\my_pr
oject_folder。那些烦人的窦bl
Ë斜杠\\。没有,才子ho
UT引号“”。
And that’s it.Now you can import from anywhere, like a normal person:from my_awesome_file import SuperCoolClass
.
就是这样,现在您可以像普通人一样从任何地方导入: 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.
没什么要添加的。 我只是希望我2个小时的沮丧+ 1个小时的撰写这篇文章可以节省您一些时间。
翻译自: https://www.freecodecamp.org/news/how-to-edit-pythonpath-on-windows-eafd19840d44/
pythonpath在哪