On the following path:
main
system.py
|-----pid
init.py
pid.py
tunning_methods.py
pid.py has the following relevant import lines:
import tunning_methods
tunning_methods.py does not import any other file as it does not need it.
init.py is empty, and system.py has the following import lines:
import pid.tunning_methods
import pid.pid
every file has a function to test it on the format:
if __name__ == "__main__":
testfunc()
now when i try to run pid.py it runs just fine, however when i try to run system.py it raises an error saying the is no such module named tunning_methods.py on the pid.py file. I was able to solve that by importing "import pid.tunning_methods" on the pid.py it was like on the system vision of import path, instead of the pid, however the pid now does not run, more than that im having a lot of trouble with imports, this is just a piece of application that goes in to a much bigger one, its possible to do this for every import, however i want an easier solution, any ideas ?
tried to do the imports on the description and it did not work as i was intending.
for anyone having the same problem the suggestion that Michael gave worked, a relative import from . import tunning_methods.
Related
I've been reading a lot of questions about this issue and still have some questions.
First of all i want to explain a little what i want to do
I have this file system
Project/
└──main.py
└──transformations/
└──__init__.py
└──translate.py
└──rotate.py
└──scale.py
And main.py being:
import transformations
if __name__ == "__main__":
print("Main:")
transformations.translate.test()
transformations.rotate.test()
transformations.scale.test()
each test() just prints "Hello" on console.
Searching i was able to somehow make it work giving __ init__.py the following command lines:
import transformations.translate
import transformations.rotate
import transformations.scale
So when i try to run the code from main.py the code is executed as intended but VSCode give me any autocomplete suggestion, so i dont know if im doing things correctly.
As you can see in this image.When i write "transformations." vscode wont give me any prompt to autocomplete "translate" "rotate" or "scale". And if i write the function call of the module anyways, it runs as intended but vscode does not recognize it as a module or function as it does with sqrt from math module as shown in the second image, where it puts "sqrt" in yellow.
So, basically, to summarize, the code is working as i want to, but im not sure if im doing things properly because the vscode autocompleter and color formatter is not detecting the scripts on the package folder.
Thanks in advance!
Because you didn't import them as modules in your __init__.py file. You can use the form from ...import... to import in the __init__.py file. like below
from transformations import translate
from transformations import rotate
from transformations import scale
Hope this helps you.
In my working directory, I have python3 files like this
/Path/to/cwd/main.py
/Path/to/cwd/Folder/one.py
/Path/to/cwd/Folder/two.py
So I had a main.py file like this
import Folder.one as one
#Do something
In one.py I had code like this
import two
#Some functions defined locally utilizing functions written in two.py
if __name__ == '__main__':
#Code for testing Functions
When I run one.py, it runs fine. But when I run main.py, it throws an error
ModuleNotFoundError: No module named 'two'
Ideally, I would not be expecting such an error at all.
It worked when I changed the import statement from import two to import Folder.two, which works. But I would like to do this in some other way without affecting such import statements much. How to achieve this?
In order for the python interpreter to know what directories contain code to be loaded, you need to include a __init__.py file.
Have a look at this answer to learn more about how to import packages.
In the case of your second import, to access that method you would need to use this syntax.
from .two import *
I have a program, program1.py, that has this structure:
Program
--program1.py
--__init__.py
--data\
----__init__.py
----helper_data.py
--classes\
----__init__.py
----helper_class.py
In helper_class.py, there is an import statement from data.helper_data import *. When I run program1, this works perfectly.
I have a second program, program2.py. I have put program1.py on my PYTHONPATH. In program2.py, I use import program1. It finds the program, but when running the imports from program1.py, I get the following error stemming from the classes.helper_class: ModuleNotFoundError: No module named 'data.helper_data'.
I think I vaguely understand what's going on, but I can't figure out the fix or the search terms to find the answer. I've tried changing the import in program1 to from ..data.helper_data import * and get an error saying I've tried a relative import beyond the parent-level package. I've also tried from .data.helper_data import * and get the same ModuleNotFoundError.
What can I do?
I think You have to import "sys" package.
import sys
sys.path.append('E:\ToDataScientist') # this is where the "Program" folder exists
from Program.data.helper_data import aa # "aa" is the class or function in helper_data
from Program.data.helper_data import * # include all from helper_data
Hello everyone im currently learning python and i im having some problems importing modules and packages. Actually i think is more of a problem with vscode.
i have this package called "paquete" with a module (funciones) that i want to import to my "main" with some fuctions in it to test if it all works correctly but i still getting "emphasized items and unresolved-import" warnings.
but for some reason it works just fine.
is more of a annoying thing.
EDIT:
module with the function "funcion"
the warning that appears in the main folder "prueba" is "emphasized items"
i tried what u guys told me to do but it stills shows the warnings
As you are trying to import a specific function from module in python
You should use in this manner:
from paquete import funciones
If you want to import full module then use:
import paquete
I can't tell whats in the funciones file. But normally this yellow import lines are telling you that you import functions, which you dont use.
Try this instead if you only want
funcion
to be imported.
from paquete.funcions import funcion
This is also better because you import only the functions you need, not all of the functions you declared in the other file. Also all imports of the other file will be loaded into your file if you import with an asterix.
The issue is you are doing all of this from within a directory named prueba. If you changed the import to from prueba.paquete.funciones import * it should work after you add a __init__.py file to your prueba directory. The other option is to use a relative import: from .paquete.funciones import *.
But do note that using import * is strongly discouraged when you are not working within the REPL. It's much better to import to the module and then reference things off the module, e.g. from prueba.paquete import funciones, from .paquete import funciones, or import prueba.paquete.funciones. That way you know exactly where things in your code came from without having to read the top of your file.
pip3 intall "name"
Use Pycharm, rather than Vscode
I am trying to import python code from one submodule to another, and I am getting an error, but only when I have more than co-dependant import from the package.
From my understanding, this "circular" importing is okay to do in Python. From the way I'd like the code to be organized, I do need these "co-dependant" imports and unless I really have to change it, I'd like to keep my code structured in the same submodules it currently is.
My directory/file structure:
./subm1/
./subm1_file.py
./subm2/
./subm2_file.py
./subm_main/
./subm_main_file.py
# subm1_file.py
# -------------
import subm_main.subm_main_file
print(subm_main.subm_main_file.test)
# subm2_file.py
# -------------
import subm_main.subm_main_file
print(subm_main.subm_main_file.test)
# subm_main_file.py
# -------------
import os
import sys
current_path = os.path.dirname(__file__)
sys.path.append(os.path.join(current_path+".."))
import subm1.subm1_file
import subm2.subm2_file
test = "test_variable"
I am running $ python subm_main_file.py while inside the subm_main directory
this works if I only use one module, so if in subm_main_file.py I comment out import subm1.subm1_file, it will run and print the test variable, and same if i comment out import subm2.subm2_file the error always comes one the second import. So in the code I show, the error is in subm2_file.py.
Error:
AttributeError: module 'subm_main' has no attribute 'subm_main_file'
It seems very strange to me that this works one time but not the second time with both import statements uncommented out. What can I do to fix this error (and hopefully keep my code organized in its current state)?
I cannot comment yet on a post but maybe your issue is that you need to place an init.py file inside the root and sub-directories if you want to keep your folder/code structure unchanged...