I am working to import a file from one directory up.
Fitv/
__init__.py
BrowserPool.py
FitvTests/
__init__.py
environment.py
So with the above file structure, I want to import BrowserPool into environment.
I started with:
from Fitv.BrowserPool import BrowserPool
And got this:
File "..\environment.py", line 4, in <module>
from Fitv.BrowserPool import BrowserPool
ModuleNotFoundError: No module named 'Fitv'
Looking at various sources I tried:
from .. import BrowserPool
I got this:
File "..\environment.py", line 3, in <module>
from .. import BrowserPool
KeyError: "'__name__' not in globals"
Tried (used absolute path):
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath('D:/Dev/Python/Fitv-master-new/Fitv/'))))
from Fitv.BrowserPool import BrowserPool
Got:
File "..\environment.py", line 3, in <module>
from Fitv.BrowserPool import BrowserPool
ModuleNotFoundError: No module named 'Fitv'
Tried (used relative path):
import sys
sys.path.append('/Fitv-master-new/Fitv/')
from Fitv import BrowserPool
Got:
File "..\environment.py", line 8, in <module>
from Fitv import BrowserPool
ModuleNotFoundError: No module named 'Fitv'
What am I doing wrong and how do I fix it?
Imports in Python are always relative to the path that the Python interpreter runs from, and from the path to environment.py shown in your trackback that includes a ..:
File "..\environment.py", line 8, in <module>
it is apparent that you are running environment.py from a sub-directory under the FitvTests, rather than the same directory where environment.py is located, in which case the .. in your import statement would simply refer to the FitvTests directory rather than its parent directory, Fitv.
You should either run environment.py from the FitvTests directory, or if you have a good reason to run it from the sub-directory you currently run it from, use ... instead to refer to the directory 2 levels up:
from ...BrowserPool import BrowserPool
Related
I have a directory tree that looks like this:
lib -
|
__init__.py
|
settings.py
|
helpers-
|
space_help.py
|
__init__.py
I need to import something from settings.py into space_help.py, however, when I try to import it and run it (using python 2.7) I get the following error:
Traceback (most recent call last):
File "dre234.py", line 10, in <module>
from lib.settings import CLONE
File "C:\Users\me\Documents\bin\python\dre234\lib\settings.py", line 9, in <module>
from lib.algorithms.step_up import *
File "C:\Users\me\Documents\bin\python\dre234\lib\algorithms\space_help.py", line 9, in <module>
from lib.settings import random_salt_generator
ImportError: No module named settings
Why am I unable to import this into the file?
When you try to import something with python, it first checks the directory of the current module, then checks the system path. If you want to import something from a folder above your current directory, you'll need to add it to the system path. Like this:
import os
import sys
file_path = os.path.join(os.path.dirname(__file__), '..')
sys.path.append(file_path)
from settings import CLONE, random_salt_generator
from algorithms.step_up import *
I know there are hundreds of post on this issue, but as fare as I see, mine is a bit different.
My project structure is the following:
package/
>script.py
>__init__.py
>submodule
>__init__.py
>otherstuff.py
>...
You should be able to run script.py simpy as a script $ ./package/script.py.
script.py needs information of submodule.__init__ so i attempt do do a relative import of submodule.
According to PEP-366 it is not possible to do relative imports, because if the script is run, __name__ will be set to '__main__' and __package__ to None.
To still use relative imports I manually set __package__ = 'package' before my imports. This allows me to properly do import package.
However from . import plotter yields
Traceback (most recent call last):
File "./script.py", line 9, in <module>
import package
File ".../package/script.py", line 13, in <module>
from . import submodule
ImportError: cannot import name submodule
if I previously imported package.
Else it yields
Traceback (most recent call last):
File "./script.py", line 12, in <module>
from . import submodule
SystemError: Parent module 'package' not loaded, cannot perform relative import
Can anyone help me how to fix the relative imports?
I don't want to use path hacks like
from os import path, pardir
from sys import path as syspath
PATH = path.abspath(path.dirname(__file__))
syspath.append(path.join(PATH, pardir))
and it is also not possible to add package to the path.
I want to run my python program from command line but it gives me below error
ImportError: No module named 'main'
My folder structure is as below
Project
|-----main
|-----__init__.py
|-----S3Operations.py
|-----BusinessOperations.py
My init.py code is as below
import sys
from S3Operations import Models
sys.path.append("D:/code/Project/main")
if __name__ == '__main__':
s3=Models()
s3.test()
And my S3Operations.py code is
import os.path
from main import BusinessService
class ModelsMlS3(object):
def test(self):
print("Testing success")
When I run the program using command line i get the below error
$ python __init__.py
Traceback (most recent call last):
File "__init__.py", line 2, in <module>
from S3Operations import ModelsMlS3
File "D:\code\Project\main\S3Operations.py", line 11, in <module>
from main import BusinessService
ImportError: No module named 'main'
Can any one please suggest a solution for the same.
You just need to do:
import BusinessService # instead of `from main import BusinessService`
as in your project, there is no __init__.py file present in Project directory (which holds main.py).
For importing it like:
from main import BusinessService
you need to create __init__.py in the folder in order to make it as module.
I have three python files, in a folder.
My folder structure looks like so:
RenderAddon (folder)
---- __init__.py
---- config.py
---- QuickRenderAddon.py
I try to run __init__.py, but I get this error:
Error:
"RenderAddon\__init__.py", line 22
from . import config
ImportError: cannot import name 'config'
Complete traceback:
Traceback (most recent call last):
File "D:\Programs\Blender_271\2.71\scripts\modules\addon_utils.py", line 299, in enable
mod = __import__(module_name)
File "C:\Users\EinarAune\AppData\Roaming\Blender Foundation\Blender\2.71\scripts\addons\RenderAddon\__init__.py", line 22, in <module>
from . import config
ImportError: cannot import name 'config'
I can't figure out why. What's wrong?
__init__.py
if "bpy" in locals():
import imp
imp.reload(config)
imp.reload(QuickRenderAddon)
print("Reloaded multifiles")
else:
from . import config
from . import QuickRenderAddon
print("Imported multifiles")
import bpy
import os
QuickRenderAddon.py
import bpy
import os
from . import config
No imports in config.py
from . import config will try to import a "config" name from __init__.py, you should instead do import config
I added the debugging print-commands and found that the path user3885927 posted didn't exist. Windows had a different language setting for those paths. Changed the OS language and now it works like a charm. Thanks, both of you
import sys
print sys.path
['D:\\zjm_code\\register2', 'C:\\WINDOWS\\system32\\python25.zip', 'D:\\Python25\\DLLs', 'D:\\Python25\\lib', 'D:\\Python25\\lib\\plat-win', 'D:\\Python25\\lib\\lib-tk', 'D:\\Python25', 'D:\\Python25\\lib\\site-packages']
and
#from django.core.management import setup_environ
from register2 import settings
#setup_environ(settings)
Traceback (most recent call last):
File "D:\zjm_code\register2\b.py", line 4, in <module>
from register2 import settings
ImportError: No module named register2
why ,
thanks
When directory 'D:\\zjm_code\\register2' is on sys.path, this means you can import modules and packages that are INSIDE that directory.
To import the directory register2 itself, two conditions:
its parent, 'D:\\zjm_code', must be on sys.path; and
file 'D:\\zjm_code\\register2\\__init__.py' must exist
__init__.py is the code that actually executes when you "import the directory".
lol because it tries to import something from register2 but it cannot since there isn't D:\zjm_code on the path..