简体   繁体   中英

Module not found when importing a class

import Stock

Whenever I try to do this I get this error

ModuleNotFoundError: No module named 'Stock'

My Stock file is located in the same folder as stock_test (this is where I do import Stock ) and I have an init file in the same folder.

I'm not really sure what I'm missing here, any help would be appreciated.

Stock file has everything that is working and I need it to import.

你试过pip install Stock吗?

I make a folder based on your discription. Below is the structure.

├── Stock.py
├── Stock_sub
│   ├── Stock_test2.py
│   └── __init.py
├── Stock_test.py
└── __init__.py

For Stock.py ,

def test():
    print('Stock.py is imported')

Now if you want to use test function in Stock_test.py , because they are in the same folder, you can import Stock just by

import Stock
Stock.test()

If you want to use Stock.py in Stock_test2.py , because they are in the different folder, so you need to specify the search path of import package

import sys
sys.path.append('../')

import Stock
Stock.test()

I use function here, the same if you want to import Class.

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