5

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.

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
  • `import learning_project.neural_networks.based_sequential as bs` – Asocia Jun 20 '20 at 21:37
  • I think you will need a file `neural_networks/__init__.py`. it can be empty. Then, you wouldn't write `import based_sequential`, you would use `import neural_networks.base_sequential` or `from neural_networks import base_sequential`. Also, keep your `learning pytorch` directory outside of your module directory. put it on the same level as `neural_networks` and `venv`. – jkr Jun 20 '20 at 21:41

2 Answers2

1

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']
Tonechas
  • 13,398
  • 16
  • 46
  • 80
  • What do you mean "returning some value"? What I'm trying to import is an object class, but the import is not working (error message in question). So no, nothing is being returned. – rocksNwaves Jun 20 '20 at 22:05
  • Hi Saif, your answer was a good start for me, but it wasn't very clear. It did eventually lead to me finding the solution, but I don't think I can accept it without it being edited for clarity. If you'd like to expand/clarify, I'd love to accept it since it really did help! – rocksNwaves Jun 23 '20 at 17:24
0

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
  • Your answer is the closest I've gotten to getting it to work. with the `..`, the module is being recognized but an error is still thrown: `ttempted relative import with no known parent package`. I'll research this error and see if I can make it work. – rocksNwaves Jun 21 '20 at 01:15
  • try changing your application into a package, it should work. – Emmanuel Oloyede Jun 21 '20 at 23:22