简体   繁体   English

即使在sys.path.append之后也无法导入python模块

[英]Can't import python module even after sys.path.append

Working on a cluster with CENTOS and Python 2.6.6 使用CENTOS和Python 2.6.6在集群上工作

Trying to add modules to a python function 尝试将模块添加到python函数

The function some_func.py looks like: 函数some_func.py看起来像:

#!/usr/bin/env python
from __future__ import division
import sys

sys.path.append('/Users/username/modules')
import some_module as sm

In the /Users/username/modules I have two files: __init__.py and some_module.py / Users / username / modules中,我有两个文件: __ init__.pysome_module.py

It works when I do it on my Mac, but does not work when I run it the CENOS machine. 当我在Mac上运行时,它可以工作,但是在CENOS机器上运行时,它不能工作。

I get the error: Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named some_module 我收到错误消息: Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named some_module

Any ideas what might be going on? 任何想法可能会发生什么?

Adding /Users/username/modules to your path and then trying to import some_module means that Python is actually looking for a directory named /Users/username/modules/some_module , which would contain (at a minimum) a file named __init__.py . /Users/username/modules添加到您的路径,然后尝试导入some_module意味着Python实际上正在寻找一个名为/Users/username/modules/some_module ,该目录将(至少)包含一个名为__init__.py的文件。

I don't know why it worked on your Mac computer. 我不知道为什么它可以在Mac电脑上使用。 Perhaps you have another copy of some_module laying around in another place, and it's finding that copy? 也许您在另一个地方放置了some_module另一个副本,并且正在寻找该副本?

Try this on your Mac: 在Mac上尝试以下操作:

import some_module
print (some_module.__file__)

And see what it reports as the full pathname. 并查看报告为完整路径名的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM