简体   繁体   中英

How to organize files properly in Python projects

I am having some difficulty with projects in Python. This is with reference to Qn 48 of Learn Python the Hard Way .

The line of the tester lexicon_tests.py that's throwing up a problem:

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 . Within lexicon , I have the file lexicon.py . Within tests , I have the file lexicon_tests.py

Is there an error in the above organization?

EDIT: Posting the code here -

In /ex48, I have 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

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. All the environment variables have been correctly added. The error I see is ImportError with the name lexicon.

Check if a file __init__.py exists in the ex48 folder. It is required to create a package and can be empty.

The error suggests that ex48 isn't in your python import search path. 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 . 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.

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:

  1. A folder named lexicon exists in the folder ex48 , and both ex48 and lexicon have an __init__.py

  2. lexicon.py is located directly in ex48 and also a __init__.py is in 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):

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.

With your current code, where you have a class lexicon in module lexicon in package lexicon , the import statement would have to look like

from ex48.lexicon.lexicon import lexicon

Actually, in your ex48 project, you'll see there's a setup.py file. 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:

'packages': ['ex48'],

Make sure in the ex48 folder, you have lexicon.py with a scan function defined. No need for a new class.

Once that's been edited nosetests should run properly with:

from ex48 import lexicon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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