我的目标很简单,我想远程获取PyPi包的依赖,而无需完全下载.
我似乎理解(阅读pip代码),当解析依赖关系时pip似乎在包下载后读取了蛋...
还有其他方法吗?
假设我有一个文件foo.py,并且在文件中我想执行一个文件bar.py. 但是,bar.py与foo.py不在同一目录中,它位于子目录调用baz中.会execfile
工作吗?怎么样os.system
?
我为自己编写了一个简单的函数,该函数可以筛选Python文件夹并查找可能的模块在哪里。我想做的很简单。我传递了一个模块导入字符串,该函数将在其中找到模块的文件夹cd,然后将其导入到我正在使用的任何环境中,例如:
anyimport('from fun_abc import *')
Run Code Online (Sandbox Code Playgroud)
最初我尝试过:
class anyimport(object):
def __init__(self, importmodule, pythonpath='/home/user/Python', finddir=finddir):
##################################################################
### A BUNCH OF CODES SCANNING THE DIRECTORY AND LOCATE THE ONE ###
##################################################################
### "pointdir" is where the directory of the file is ###
### "evalstr" is a string that looks like this : ---
### 'from yourmodule import *'
os.chdir(pointdir)
exec evalstr
Run Code Online (Sandbox Code Playgroud)
当我在iPython Notebook中编写整个代码时,它就可以工作。所以问题就由我溜走了。然后我发现它不能正常工作,因为函数导入的模块保留在函数的局部变量空间中。
然后,我发现了这个Stack Overflow讨论:“在Python中,为什么在函数中的exec中导入无效?” 。因此,我将代码更改为以下内容:
class anyimport(object):
def __init__(self, importmodule, pythonpath='/home/user/Python', finddir=finddir):
##################################################################
### A BUNCH OF CODES SCANNING THE DIRECTORY …
Run Code Online (Sandbox Code Playgroud)