I did everything to resolve this issue.
I am a beginner with python. My Folder structure:
├─conversational-datasets-master-opensubtitles/
└── conversational-datasets-master/
└── opensubtitles/
└── create_data.py
└── create_data_test.py
in opensubtitles folder, I have two files
The first one is create_data.py which is for the train
the second one is create_data_test.py which is for testing
So the Question is
Whenever I am trying to run the create_data_test.py it shows this error (module not found error)
File "C:\Users\suppo\Desktop\conversational-datasets-master-opensubtitles\conversational-datasets-master\opensubtitles\create_data_test.py", line 15, in <module>
import opensubtiles.create_data.py
ModuleNotFoundError: No module named 'opensubtiles'
Try changing your import statement in create_data_test.py
from:
import opensubtitles.create_data.py
to:
import create_data
I think you just got a typo here ...
import opensubti t les.create_data.py
There are several posts of importing modules and classes in python. Take a look here... hope it helps
How to import the class within the same directory or sub directory?
from opensubtitles import create_data
from opensubtitles import create_data_test
This should work.
But if you are in the root folder, you may have to use joinpath, since there are hyphens in the folder names.
Related
I am trying to work with python packages and modules for the first time and come across some import errors I don't understand.
My project has the following structure:
upper
├── __init__.py
├── upper_file.py # contains "from middle.middle_file import *"
└── middle
├── __init__.py
├── middle_file.py # contains "from lower.lower_file import Person, Animal"
└── lower
├── __init__.py
└── lower_file.py # contains the Classes Person and Animal
I can run middle_file.py and can create inside the file a Person() and Animal() without any problems.
If I try to run upper_file.py I get a ModuleNotFoundError: No module named 'lower' error.
However, I have no trouble importing Animal() or Person() in upper_file.py directly with from middle.lower.lower_file import *
If I change the import statement inside middle_file.py from from lower.lower_file import Person, Animal to from middle.lower.lower_file import Person, Animal I can successfully run upper_file.py but not middle_file.py itself (and pycharm underlines the import in middle_file.py red and says it doesn't know middle)
In the end, I need to access inside of upper_file.py a class that is located inside of middle_file.py, but middle_file.py itself depends on the imports of lower_file.py.
I already read through this answer and the docs but just don't get how it works and why it behaves the way it does.
Thanks for any help in advance.
You should use relative import to accomplish this. First link on Google I found some practical example that could help you understand better.
On middle_file try to use from .lower.lower_file import *. It should solve the issue on upper_file.
I am currently working on a project: a little game, text-based.
I made a folder called game, with all my files related to this project. I have several, and even the python code is split into a few files to make it easier for me.
My directory looks like this:
C:.
boucle_de_jeu.py
clss.py
documentation.md
fonctions_boucle_jeu.py
idees.txt
map_developpement.py
meta_data.json
-->boucle_de_jeu.py is the main script, it contains the game loop
-->clss.py contains the classes of the game's items
-->fonctions_boucle_jeu.py contains a few functions used in boucly_de_jeu.py
-->map_developpement.py is the file where I create all the instances of the game, with the classes I imported from clss.py
I tried to run the main file boucle_de_jeu.py, and I got this:
PS C:\Users\...\python\game> py boucle_de_jeu.py
Traceback (most recent call last):
File "C:\Users\...\python\game\boucle_de_jeu.py", line 6, in <module>
import game.map_developpement as map_items
ModuleNotFoundError: No module named 'game'
and these are the first lines of my boucle_de_jeu.py file:
# ...
import sys
from os import system
# ...
import game.map_developpement as map_items
import game.fonctions_boucle_jeu as fct_boucle
My question is, what are the ways to make this work? Maybe I should also reorganize my folder?
Thanks in advance for your precious help, have a nice day^^.
I believe that you mean to say all these files are in a folder called game, so the structure really looks like
game/
├── boucle_de_jeu.py
├── clss.py
├── documentation.md
├── fonctions_boucle_jeu.py
├── idees.txt
├── map_developpement.py
└── meta_data.json
Importing files in this manner can be a little tricky and you should be clear about what you hope to achieve. If this game/ folder is in fact a module that will be used by other projects in the future, then (you need to include an __init__.py file, and) to import from other files in that module you should use the convention.
from .map_developpement as map_items
However, if this is the entire project and you are only going to run files in this folder from within the folder, you can use the suggestion made by #jr15:
from map_developpement as map_items
Note that these are not mutually compatible. If you wish to use the first option you will need to have all of your testing and whatnot outside of the game/ folder and you will not be able to run files directly from the game/ folder. If you go with the second option you will be unable to use the games/ folder as a separate module.
To import from game, you should have a file called game.py. I don't see one in your file list there.
Instead of this:
import game.map_developpement as map_items
Just do:
import map_developpemennt as map_items
I have the following directory structure for my project
project_Rosa
Rosa
__init__.py
filehandle
__init__.py
fmain.py
punnet
__init__.py
pmain.py
sequences
__init__.py
smain.py
current_code.py
Rosa is the main package I am developing and I am testing the imports from current_code.py. All the __init__.pys are empty right now. fmain.py module has a function string_fasta(). The problem is I can directly import it in current_code.py with
import Rosa.filehandle.fmain as f
function = f.string_fasta
or by
import Rosa.filehandle.fmain.string_fasta as function
but when I write this
import Rosa.filehandle as R
a = R.fmain.string_fasta
I get an error
module 'Rosa.filehandle' has no attribute 'fmain'
Please address what is wrong here, and how to call names from modules shallower or deeper than the current script inside it ?
I have searched related questions and tested many solutions there, for example,
I found [a solution here]:https://askubuntu.com/a/470989/608783, but I found it does not work in my case...
I have the following file structure:
Folder1
Folder2
__init__.py
model.py
Folder3
__init__.py
test.py
In model.py I defined a class:class mymodel(), then in test.py I wanna to use mymodel, so I tried from ..Folder2 import mymodel, from ..Folder2.model import mymodel, but all failed with error ValueError: Attempted relative import in non-package.
What should I do? Thanks for any advice.
Edit: I am very sorry for not list out _init_.py files, they are indeed included in the Folder2 and Folder3. However, the content of them are different from which proposed by #anekix's solution.
The following are from my possibly wrong setting:
__init__.py in Folder2: from .model import mymodel
__init__.py in Folder3: empty
What is wrong with my two __init__.py, thank you again.
Update: One way to solve this problem is by set all __init__.py to empty and then follow #jeffy proposed solution. Another way is by #anekix proposed solution, which should also works for this kind of problem(you should have a try) although not in my case, possibly because the whole project of mine is too messy.
This could work:
import sys
sys.path.append("/path/to/Folder1")
from Folder2.model import classname
you should make both subfolders as a package(by creating __init__.py in both subfolders) for proper management and imports:
structure should be:
Folder1
Folder2
__init__.py
model.py
Folder3
__init__.py
test.py
inside folder2/__init__.py add this code below:
from model import *
# it imports everything defined in model.py so that
# you can access classes or functions from this python file
inside folder3/__init__.py folder3 add this code below:
from test import *
# it imports everything defined in test.py
Then from inside folder3 you can use the import as(assuming you have some file main.py in folder3) :
from folder2 import someClass
# someClass is a class defined in `model.py`
So steps are:
make `folder2` a package.
make `folder3` a package.
import using `folder1.someClass` or `folder2.someClass`.
I am having trouble using my classes that I've defined in a module. I've looked at this stackoverlfow post, and the answer seems to be "you don't need imports." This is definitely not the behavior I'm experiencing. I'm using Python 3.3. Here is my directory structure:
root/
__init__.py
mlp/
__init__.py
mlp.py
layers/
__init__.py
hidden_layer.py
dropout_layer.py
My problem is this: the class defined in dropout_layer.py extends the class in hidden_layer.py, but when I try to import hidden_layer, I sometimes get an error depending on the directory I execute my code from. For instance, from layers.hidden_layer import HiddenLayer then I run my code if I execute it from root/mlp. This import does not work, however, if I execute my code from root. This is strange behavior to me. How can I get this working correctly?
My only non-empty __init__.py file is in root/mlp/layers/:
# root/mlp/layers/__init__.py
__all__ = ['hidden_layer', 'dropout_layer']
In Python 3 you can prepend a . for an import relative to the location of the current module:
from .hidden_layer import HiddenLayer