Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 837
rocksNwaves Avatar asked Nov 06 '22 06:11

rocksNwaves


1 Answers

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']
like image 125
saif mathur Avatar answered Nov 14 '22 23:11

saif mathur