Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I import module from different file in same directory? [duplicate]

Tags:

module

rust

My directory structure:

src
    main.rs
    image.rs
    decoders.rs

When I try to import my decoders module in image.rs I get this:

error[E0583]: File not found for module `decoders`

decoders.rs:

pub mod Decoders {}

image.rs:

mod decoders

use decoders::Decoders

pub mod Image {}

Note: I am using a module that wraps the entire file on purpose that's I can put attributes on entire files. This is why it's not a duplicate of How to include module from another file from the same project?

The weird thing is, is that this syntax works perfectly fine when I try to import Image in main.rs:

mod image;

use image::Image;
like image 495
TheGreyBearded Avatar asked Apr 26 '19 13:04

TheGreyBearded


People also ask

Can we import a module twice?

The rules are quite simple: the same module is evaluated only once, in other words, the module-level scope is executed just once. If the module, once evaluated, is imported again, it's second evaluation is skipped and the resolved already exports are used.

How can I import modules if file is not in same directory?

We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory.

How do I import files into the same folder?

Make an empty file called __init__.py in the same directory as the files. That will signify to Python that it's "ok to import from this directory". The same holds true if the files are in a subdirectory - put an __init__.py in the subdirectory as well, and then use regular import statements, with dot notation.

Can we import multiple modules with single import statement?

Import multiple modulesYou can write multiple modules separated by commas after the import statement, but this is not recommended in PEP8. Imports should usually be on separate lines. If you use from to import functions, variables, classes, etc., as explained next, you can separate them with a comma.


1 Answers

What's happening is that when you try to import decoders::Decoders in image.rs, you need to go through the next level up, because using this:

mod decoders

use decoders::Decoders

Means that decoders will now be "owned" or under image, which means that the compiler will search in the image subdirectory for decoders.rs. So, to fix this, you can either change your file structure to this:

src/
    main.rs
    image.rs        ** or image/mod.rs
    image/
        decoder.rs

Or, use this in main.rs:

mod decoders;
mod image;

and this in image.rs:

use super::decoders::Decoders;
//Or alternatively
use crate::decoders::Decoders;

Also, to fix your nested-mod problem, do the following in decoders.rs:

//Your code, no `mod Decoders`

and the following where you have your mod decoders statement:

#[your_attribs]
mod decoders;
like image 154
Optimistic Peach Avatar answered Sep 18 '22 13:09

Optimistic Peach