简体   繁体   中英

Python cannot run module from command line; IPython can

This is the directory structure for my modules: Directory Structure

As can be seen, DataProcessor is a module inside which DataLoader has certain functions.

Inside the lda module, the file HFT.py have the line from DataProcessor import DataLoader

I am trying to run lda/HFT.py from the main directory 274-Yelp/

python lda/HFT.py
this gives the following error:

Traceback (most recent call last):
  File "lda/HFT.py", line 6, in <module>
    from DataProcessor import DataLoader
ImportError: No module named DataProcessor

However, when I run
ipython lda/HFT.py
it runs!

I am using a virtualenv located in the main directory 274-Yelp/

Can someone tell me why Ipython can import the module but normal Python interpreter can't?

Contents of DataProcessor/__init__.py :

import DataLoader
import MatrixConstructor
import ReviewProcessor
import vocabBuilder

Contents of lda/__init__.py :

from ReviewModel im

port ReviewModel
from RatingModel import RatingModel
from HFT import HFT

The path calculation for your python scripts is being affected by initializers or cd directory, or your ipython is actually launching a different python binary. Given that your ipython is pointing into your virtualenv directory I am guessing its the former.

Normally the directory you're running from is added to the sys.path for you benefit, but it looks like you might be running this from Eclipse given the screenshot. That has its own current directory settings for each run that you can configure under run settings.

You might have added the working directory into your .ipython file (found via ipython locate ) which would only help ipython runs.

To fix the issue either add the path to your project root directory to the top of both init files as a sys.path.insert(1, 'my/root/path') , or combine the separate modules into a single module with relative importing from ..DataProcessor import DataLoader . This required that the shared parent directory have an __init__.py file but allows run something like python -m shareddir.lda.HFT to always know about the relative path to other modules.

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