I was trying to import a function from a sibling folder and i could, i look for documentation and didnt found something useful, can you help me please?
I have a folder named fold1, inside i have fold2 and fold3, inside fold2 i have filetoimport.py, it has:
def fntest(a, b):
return print(a+b)
And i want to import that function from inside fold3 file main.py, there i have
from fold2 import filetoimport
filetoimport.fntest(2,2)
Then i get an error:
Traceback (most recent call last):
File "fold3/main.py", line 1, in <module>
fold1 import filetoimport
ImportError: No module named 'fold1'
Hope you can help me :)
I also tried
from fold1.fold2 import filetoimport
But it failed.
Alright, this should work
import sys
import os
sys.path.insert(1,os.popen('pwd').read().replace('3\n','2'))
import filetoimport
filetoimport.fntest(2,2)
you just need to add the target folder to the python runtime path
You need to import the parent folder as a path the program can check inside for more modules. I had some luck with this
import os
import sys
sys.path.append(os.path.realpath('..'))
from fold2 import filetoimport
filetoimport.fntest(2,2)
As far as I have understood, your folder structure is as below
fold1
fold2
-- filetoimport.py
fold3
-- main.py
It seems that your are executing the main.py file from terminal (by opening fold1 path) as
python3 fold3/main.py
Instead you should execute the command as
python3 -m fold3.main
Since the argument is a module name, you must not give a file extension (.py)
On the other hand if you open fold1 as a project in IDE like pycharm or intellij idea and execute main.py via IDE itself, it wont give you an error.
Hope, this will solve your problem.
Related
I am building a software. I organised the different code in differents python files (the modules) organised in different folders.
It was working like a charm. Then today it doesn't work anymore as I have some issues:
ModuleNotFoundError: No module named 'modules.mymodulesteam'
This is how my code is organized:
MySoftware/
- start.py
- modules/
-- mymodules.py
-- prepare.py
-- mymodulesteam.py
In the start.py file, I have this import:
from modules import prepare_envir_appium
in prepare.py I have this line of import
import modules.mymodulesteam as mymodules
When I execute "start.py", I have this error message in the console:
Traceback (most recent call last):
File "E:\MySoftware\modules\prepare.py",
import modules.mymodulesteam as mymodules
ModuleNotFoundError: No module named 'modules.mymodulesteam'
so I search for help and I saw I had to add some _init.py file in my folder "modules". I did it and it didn't work. I get the exact same issue.
I read every single post regarding this kind of issue and tried much stuff.
from modules import mymodulesteam
from . import mymodulesteam
etc...
And I get this kind of errors:
Traceback (most recent call last):
File "E:\MySoftware\modules\prepare.py",
from modules import mymodulesteam as mymodules
ImportError: cannot import name 'mymodulesteam' from 'modules' (E:\MySoftware\modules\__init__.py)
So I removed this "_ init _.py" file and tested again. I get this error this time:
Traceback (most recent call last):
File "E:\MySoftware\modules\prepare.py",
from modules import mymodulesteam as mymodules
ImportError: cannot import name 'mymodulesteam' from 'modules' (unknown location)
It used to work very well. I don't know what happened. Does anyone can help me please?
It doesn’t work because the modules are not in the same path and the script doesn’t know where to look.
Your current start.py file looks like this,
from modules import prepare_envir_appium
Instead, do this at the top of your start.py file, because your prepare_envir_appium.py file is located in modules.
import sys
sys.path.insert(0,"../modules")
import prepare_envir_appium
Now you can access your functions using prepare_envir_appium.functionname.
You can also do this,
import sys
sys.path.insert(0,"../modules")
import prepare_envir_appium as per
Now you can access the functions using per.functionname within start.py.
You can also import specific functions like this,
import sys
sys.path.insert(0,"../modules")
from prepare_envir_appium import functionname1, functionname2
and you can just run them using their function names.
However, if you are looking at the prepare.py script, because the module is in the same folder as the script, all you have to do is import the file name like below without the .py extension.
You can follow the same import naming conventions as above.
import mymodulesteam
or
import mymodulesteam as mmt
or
from mymodulesteam import function1, function2
I am trying to create a package for my own use and cannot seem to be able to import a class into another folder. Here is a image of my current directory.
I am trying to import a class from strategies.core into my test/test_symbol.py file but it keeps giving me an ModuleNotFoundError
Error
Traceback (most recent call last):
File "c:\Users\Francois\Desktop\H4Impulse\test\test_symbol.py", line 6, in <module>
from strategies.core import Symbol
ModuleNotFoundError: No module named 'strategies'
test_symbol.py
import unittest
# import sys
# sys.path.append(r"C:\Users\Francois\Desktop\H4Impulse")
from strategies.core import Symbol
# from ..strategies.core import Symbol <--- this also doesn't work
class TestSymbol(unittest.TestCase):
pass
unittest.main()
it works when I use sys.path.append to append the path but I feel like this shouldn't be necessary. Any help would be appreciated.
Problem lies in your path. You are using:
absolute path in sys.path.append
relative path in your import
Probably if you call sys.path.append(r"./") it would raise the same ModuleNotFoundError error.
Since you are running the script "c:\Users\Francois\Desktop\H4Impulse\test\test_symbol.py", your working directory is probably set as "c:\Users\Francois\Desktop\H4Impulse\test". In this folder, the module strategies does not exist.
Finally, you should either:
Set "c:\Users\Francois\Desktop\H4Impulse" as your working directory (suggested if it is THE directory of your project)
Use relative imports
I have a problem. I created a script that uses a few functions, but now I have moved those functions in a folder named: include. The file is called: mylib.py, but when I use the following code:
import sys
sys.path.insert(0, 'include/')
import mylib
It gives an error: No module named 'mylib'. The main code is in the windows directory: Desktop/Python/ and the include file in: Desktop/Python/include/.
What am I doing wrong?
add an empty file __init__.py to the include folder to make it a package.
then import from it with:
from include import mylib
Replace the second line with this one:
sys.path.insert(0, '/Desktop/Python/include')
I don't find the mistake in the code nor in the call when trying an import test.
My directory structure is the following: (from /home/user1/python_test/)
main.py
Aux/lib1.py
Aux/__init__.py
main.py:
from Aux.lib1 import fun_A
if __name__ == "__main__":
fun_A()
print("all done")
Aux/lib1.py:
def fun_A():
print("A function called")
I'm executing from terminal (python main.py from directory where main.py is), maybe is there a need to set pythonpath? I don't recall the need before, I've made some python programs like these some time ago (2/3 years)
I have also tried from .Aux.lib1 import fun_A instead of from Aux.lib1 import fun_A but nothing works. The error is:
File "main.py", line 1, in <module>
from Aux.lib1 import fun_A
ImportError: No module named Aux.lib1
Create a blank file with name __init__.py under the folder Aux. Without this file a directory can not be a module.
Oh my, It was a super silly mistake. I'm so used to execute inside a virtual environment that I was using python2 by mistake using python main.py. Executing python3 main.py worked for me.
I write some python files like this:
main.py
view/ __init__.py #empity file
MainWindow.py
ListEditor.py
And in each file I wrote those imports:
<main.py>
from view.MainWindow import MainWindow
...
-
<MainWindow.py>
from view.ListEditor import ListEditor
and ListEditor.py don't import any files.
Each MainWindow.py or ListEditor.py defines a class that named same as the file name.
when I run the program from main.py, it works. But when I run from MainWindow.py I got ImportError: No module named 'view'
If I write
from ListEditor import ListEditor
in MainWindow.py, python MainWindow.py will be OK. but python main.py will get error:
ImportError: No module named 'ListEditor'
So, is there a way to make both python main.py and python MainWindow.py get right at the same time?
I'm using python3.4
P.S.
I think I have figured out the problem here. The import command searches a module in sys.path. The sys.path is a group of predefined paths plus the running script path. When I run the code from MainWindow.py, the code import ListEditor just works, but when I run from main.py, the current path is set to the parent path. So I need import view.ListEditor.
Well, there are couple ways to deal with it. #Vincent Beltman's answer is one of it. Or just put these code in the __init__.py file:
import os, sys
path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(path)
Finally, I'm new to python. And I think the import command is quite strange. I thought it should search the files relative to the path of the source file that containing the command, not just relative to the starter file. A starter file may varying and cause troubles like this one.
Try this:
try:
from view.ListEditor import ListEditor # If this one fails
except:
try:
from ListEditor import ListEditor # It will try this one