简体   繁体   中英

The following module won't import

I've got a PyCharm "project" in Python, which is to say that I have a folder that is a conglomeration of all sorts of experimental python files, convenience methods/classes, and Jupyter notebooks from following along with online classes.

I've actually just written something that I'm proud of and would like to re-use. I'm finding it difficult to import. I have looked at and attempted implementing the answers to the following questions to no avail:

  1. Can't import my own modules in Python
  2. How to import my own modules in python 3.6?
  3. How to import my own modules the elegant way?

Project structure:

learning_project
|
├───.idea
│   ├───dictionaries
│   └───inspectionProfiles
|
├───decision_trees
├───linear_algebra
├───neural_networks
|   ├───based_sequential.py <---------------------------- # Module location #
│   ├───cross-entropy-gradient-descent
│   └───learning pytorch 
|       ├─── class_notebook.ipynb <---------------------- # Want to import for use here #
|       └───Cat_Dog_data  
|
└───venv
    ├───Include
    ├───Lib
    │   └───site-packages
    └───Scripts

I have tried the following:

import based_sequential
from based_sequential import ClassName
import based_sequential.ClassName
import neural_networks
from neural_networks import based_sequential
import neural_networks.based_sequential
from neural_networks.based_sequential import ClassName

All result in the error No module named '<pick your poison>'

  • Question 1: Obviously, what am I missing?
  • Question 2: Is my organization here part of the problem? I'm starting to suspect that it is.

I also suspect I have some work to do learning the administrative aspects of writing code that is bigger than just one.py file.

I hope you're returning some value in the function/module you're trying to import. If not, check that.

Otherwise, just use sys.path from sys module and direct it towards the file you want to import.

>>> import sys
>>> sys.path
   ['',
   'C:\\Python33\\Lib\\idlelib',
   'C:\\Windows\\system32\\python33.zip',
   'C:\\Python33\\DLLs',
   'C:\\Python33\\lib',
   'C:\\Python33',
   'C:\\Python33\\lib\\site-packages']

I suggest you check on relative imports. https://realpython.com/absolute-vs-relative-python-imports/ The way you can solve this problem is shown below:

FILE STRUCTURE I'LL BE USING:

└── project
    ├── package1
    │   ├── module1.py
    │   └── module2.py
    └── package2
        ├── __init__.py
        ├── module3.py
        ├── module4.py
        └── subpackage1
            └── module5.py

To import from module 2 while in module 1 use:

from .module2 import function1

In the case of importing the module3 from module2 use:

from ..package2 import module3

IN YOUR OWN CASE, IT SHOULD BE:

from ..based_sequential import whatever

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