Python导入模块:如何跟踪它的orgin PYTHONPATH,sys,os(Python importing a module: how to track its orgin PYTHONPATH, sys, os) python

模块nosetests在我的计算机上运行(它不应该只在几个指定的地方运行)。 我想这是因为我不小心将模块测试添加到PYTHONPATH中,或者直接将它放在dist-packages或site-packages中,或者让python每次都永久地查找它。

我熟悉一些命令,如find,import os,import sys和PYTHONPATH,但我似乎找不到跟踪罪魁祸首目录的方法,这样就可以实现这一点。

就像是

>>> find . -name "*nosetests"* -print

任何帮助都会很棒。

the module nosetests runs everywhere on my computer (it shouldn't it should only run in a few specified places). I guess this is because i have accidentally added the module nosetests to the PYTHONPATH either by putting it directly in either the dist-packages or site-packages or telling python to look for it permanently everytime.

I'm familiar with a few commands like find, import os, import sys and PYTHONPATH but i can't seem to find a way track down the culprit directory thats allowing this to happen.

something like

>>> find . -name "*nosetests"* -print

any help would be great.

最满意答案

为了解决你的问题,就像@Adam建议的那样检查nosetests.__file__就足够了。 但是有一种更通用的方法: inspect模块,它也适用于没有__file__属性的类和其他对象。

import inspect, nosetests print inspect.getsourcefile(nosetests)

To solve your problem it's enough to examine nosetests.__file__, as @Adam suggested. But there's a more general way: the inspect module, which also works for classes and other objects that don't have a __file__ attribute.

import inspect, nosetests print inspect.getsourcefile(nosetests)Python导入模块:如何跟踪它的orgin PYTHONPATH,sys,os(Python importing a module: how to track its orgin PYTHONPATH, sys, os) python

模块nosetests在我的计算机上运行(它不应该只在几个指定的地方运行)。 我想这是因为我不小心将模块测试添加到PYTHONPATH中,或者直接将它放在dist-packages或site-packages中,或者让python每次都永久地查找它。

我熟悉一些命令,如find,import os,import sys和PYTHONPATH,但我似乎找不到跟踪罪魁祸首目录的方法,这样就可以实现这一点。

就像是

>>> find . -name "*nosetests"* -print

任何帮助都会很棒。

the module nosetests runs everywhere on my computer (it shouldn't it should only run in a few specified places). I guess this is because i have accidentally added the module nosetests to the PYTHONPATH either by putting it directly in either the dist-packages or site-packages or telling python to look for it permanently everytime.

I'm familiar with a few commands like find, import os, import sys and PYTHONPATH but i can't seem to find a way track down the culprit directory thats allowing this to happen.

something like

>>> find . -name "*nosetests"* -print

any help would be great.

最满意答案

为了解决你的问题,就像@Adam建议的那样检查nosetests.__file__就足够了。 但是有一种更通用的方法: inspect模块,它也适用于没有__file__属性的类和其他对象。

import inspect, nosetests print inspect.getsourcefile(nosetests)

To solve your problem it's enough to examine nosetests.__file__, as @Adam suggested. But there's a more general way: the inspect module, which also works for classes and other objects that don't have a __file__ attribute.

import inspect, nosetests print inspect.getsourcefile(nosetests)