[英]How to organize files properly in Python projects
I am having some difficulty with projects in Python. 我在使用Python的项目时遇到了一些困难。 This is with reference to Qn 48 of Learn Python the Hard Way . 这是参考《艰苦学习Python》的 Qn 48 。
The line of the tester lexicon_tests.py
that's throwing up a problem: 测试人员lexicon_tests.py
这一行lexicon_tests.py
了一个问题:
from ex48 import lexicon
The error I am seeing is: 我看到的错误是:
ImportError: no module named ex48
I wonder if this is because I haven't organized my files properly within the projects folder: I have a folder named ex48
, subfolders of which include tests
and lexicon
. 我想知道是否是因为我没有在projects文件夹中正确地组织我的文件:我有一个名为ex48
文件夹,其子文件夹包括tests
和lexicon
。 Within lexicon
, I have the file lexicon.py
. 在lexicon
,我有lexicon.py
文件。 Within tests
, I have the file lexicon_tests.py
在tests
,我有lexicon_tests.py
文件
Is there an error in the above organization? 上述组织有错误吗?
EDIT: Posting the code here - 编辑:在这里发布代码-
In /ex48, I have setup.py 在/ ex48中,我有setup.py
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
config = {
'description': 'My Project',
'author': 'MyName',
'url': 'URL to get it at.',
'download_url': 'Where to download it.',
'author_email': 'My email.',
'version': '0.1',
'install_requires': ['nose'],
'packages': ['ex48'],
'scripts': [],
'name': 'projectname'
}
setup(**config)
in /ex48/lexicon, I have lexicon.py 在/ ex48 / lexicon中,我有lexicon.py
class lexicon:
@staticmethod
def scan(string):
direction = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back']
verbs = ['go','stop','kill','eat']
stop = ['the','in', 'of', 'from', 'at','it']
nouns = ['door', 'bear', 'princess', 'cabinet']
words = string.split()
result = []
for word in words:
if word in direction:
result.append(('direction',word))
and so on . 等等 。 . 。 . 。 with return result
at the end. 最后return result
。 All the environment variables have been correctly added. 所有环境变量均已正确添加。 The error I see is ImportError
with the name lexicon. 我看到的错误是ImportError
,名称为lexicon。
Check if a file __init__.py
exists in the ex48 folder. 检查ex48文件夹中是否存在文件__init__.py
。 It is required to create a package and can be empty. 创建一个包是必需的,可以为空。
The error suggests that ex48 isn't in your python import search path. 该错误表明ex48不在您的python导入搜索路径中。 You can check that by doing: 您可以通过以下方法进行检查:
import sys
sys.path
EDIT 编辑
Here is a step by step tutorial of adding paths to python's import search path: Set up Windows Python Path system environment variable . 这是向Python的导入搜索路径添加路径的分步教程: 设置Windows Python Path系统环境变量 。 I'm guessing you didn't add them properly if they still don't appear in sys.path
and until they do that import has no reason to work. 我猜想如果它们仍然没有出现在sys.path
并且您没有正确添加它们,并且直到它们执行该导入操作就没有理由。
EDIT 编辑
Now after your new errors. 现在,在出现新错误之后。 When you do from ex48 import lexicon
one of the following should be true in order for that to work: 当您from ex48 import lexicon
执行操作时,以下条件之一必须正确才能使其正常工作:
A folder named lexicon
exists in the folder ex48
, and both ex48
and lexicon
have an __init__.py
一个文件夹命名lexicon
存在于文件夹ex48
, 都 ex48
和lexicon
有一个__init__.py
lexicon.py
is located directly in ex48
and also a __init__.py
is in ex48
. lexicon.py
直接位于ex48
而__init__.py
也位于ex48
。
EDIT 编辑
The error you say you get from your last comment are caused by bad identation. 您说从最近的评论中得到的错误是由错误的标识引起的。 The code you posted above needs and extra identation level for each line below def scan(string):
您在上面发布的代码需要和def scan(string):
下面每一行的额外标识级别def scan(string):
For this 为了这
from ex48 import lexicon
result = lexicon.scan("north south east")
to work, you should put lexicon.py
in the folder ex48
, and lexicon.py
should contain a scan
function at module level, not as a class method. 要工作,您应该将lexicon.py
放在文件夹ex48
,并且lexicon.py
应该在模块级别包含scan
功能,而不是作为类方法。
With your current code, where you have a class lexicon
in module lexicon
in package lexicon
, the import statement would have to look like 根据您当前的代码,其中你有一个类lexicon
模块lexicon
封装lexicon
,import语句将不得不像
from ex48.lexicon.lexicon import lexicon
Actually, in your ex48 project, you'll see there's a setup.py file. 实际上,在ex48项目中,您会看到有一个setup.py文件。 In there, you'll see the line: 在其中,您将看到以下行:
'packages': ['NAME'],
What you'll want to do is change NAME to your folder name (ex48) so it looks like this: 您需要做的就是将NAME更改为您的文件夹名称(ex48),如下所示:
'packages': ['ex48'],
Make sure in the ex48 folder, you have lexicon.py with a scan function defined. 确保在ex48文件夹中,您具有lexicon.py,其中定义了扫描功能。 No need for a new class. 无需上新课。
Once that's been edited nosetests should run properly with: 编辑完成后,鼻子测试应正确运行:
from ex48 import lexicon
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.