简体   繁体   中英

Python: program called from another file not running correctly

Among the bugs I have found and solved so far, my actual code is pretty clean and runs good. I think I have narrowed down a problem I am having to the following... I have a folder, "Stocks", that contains python code and another nested folder, "PullStock", with more python code and text files within it.

Filesystem example:

Folder: Stocks
  python code files (`stockcalc.py`)
  Folder: PullStock
    python code files (`pullList.py`)
    text files

I can run the program pullList.py by itself and it runs fine. All it does is parse lines of a text file and store the variables into an array. The output when I run this program alone is provided below:

array length in pullList:  2

Now when I run stockcalc.py , I import the file pullList.py from the folder 'PullStock' and it does not run the same. The output is below:

array length in pullList:  0

My header of stockcalc.py looks like this:

#!/usr/bin/env python
from PullStock import pullList
#import PullStock.pullList

I have tried both of these solutions above in my header and neither of them seems to work. If I am calling the file, then shouldn't it run the same as when I run it by itself? If a solution can not be figured out from the above information, then I am sorry for wasting your time and I have so more work to do.

You've got at least multiple problems. Start here:

from StockList import pullList

Your directory is called PullStock , but you're trying to use the name StockList . That's obviously not going to work.

But, even if you fix that, you've got a bigger problem: StockList is not a package. In Python 2 (which it looks like you're using), a directory is only a package if it has a file named __init__.py in it. It can be empty, but it has to exist.

It's almost always wrong to run a script inside a package directory, so by fixing that, you're also breaking the part that worked. You may get away with it anyway, but you should look into a better organization—eg, a __main__.py file that lets you run the package as a script, or a "launcher" script at the top level.

I'd lay 50/50 odds that you have one more problem: if pullList.py is opening 'spam.txt' and 'eggs.txt' and just expecting that to work, it won't work when your working directory isn't the script's directory; you'll want to add scriptdir = os.path.abspath(os.path.dirname(sys.argv[0])) to the top of the script, and then use os.path.join(scriptdir, 'spam.txt') , etc.

And finally, it's usually a good idea to use all-lowercase names for your modules. Especially if you want the same code to run on both Windows and non-Windows systems.

If you want to work with multiple file organization and creating packages and importing modules, you should have a look at the tutorial in Python Doc. https://docs.python.org/2.7/tutorial/modules.html#packages

There you will find a good explanation of how you should organize your packages and what you need to add as file( __init__.py ) to make that folder recognized as a module...etc.

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