Cannot find module when importing from project in python - python

My directory structure looks like this:
I have some utility functions in util/misc.py that I want to import in compose_dataset.py. However, I cannot get the import statement to work.
I'm working on Windows Python3.5.4, so from what I've read I don't need __init__.py files anymore. The project folder is a child of my PYTHONPATH that points solely to E:\Python. So far, I tried:
from misc import *
from util import *
from util.misc import *
from ..util.misc import *
and either received ImportError: No module named 'xyz' or ImportError: attempted relative import with no known parent package. I also tried adding init-files but I have no experience with those and simply added one to every directory, but (surprisingly) that didn't work either.
What am I missing here??

Try this:
import sys
sys.path.insert(0, '../')
from util.misc import *
You may also want to take a look at this post: How to access a module from outside your file folder in Python?

Set your PYTHONPATH to ., and add __init__.py files into each directory, where you want to import from, in your case add it into src, data, util directories. Assuming that you run your "entry point" script from the root directory of your project (same where your README.md file is), to import use this code:
# compose_dataset.py file
from src.util.misc import function_name

Related

ModuleNotFoundError when trying to import file from one folder into another file located in another directory

I working with a group on a project that is being used with Git and Github. I have a local main project folder that contains two sub-directories(i.e Utilities and Testing). However, in order to run the test files inside the directory project/Testing, I need to import files from the Utilities directory itself. I am familiar with some python techniques of importing for example: from . import or from import * or import . However, as I am trying to use these techniques in my testing.py file located in my testing folder, I run into a moduleNotFoundError: No modules named "Utilities" and currently both directories contain an init.py file.
I tried doing something like this...
from Utilities.deck import Deck and from Utilities import * however both seems to not be working. I came across adding the init.py file inside both directories, and thus the init file for Utilities contains the following...
from .card import *
from .deck import *
from .player import *
However that did not work either. I am not really sure what else to do to solve this issue and could really use some help and guidance that will work for me and my team to avoid this issue in the near future.
Append parent path by adding this line before the imports in the files in Testing:
import path
import sys
# directory reach
directory = path.Path(__file__).abspath()
# setting path
sys.path.append(directory.parent.parent)
Then this import should work:
from Utilities.deck import Deck

Import Error Attempted relative import when importing from another folder

It might be a pretty simple error but I don't get to solve it. The issue is that I have two folders, folder1 and utils. Inside folder one, I am trying to import a function from utils.py script in utils folder. Both folders have their respective init files. However, I get this import error and I don't know why.
Folder1
Script1.py
utils
utils.py
The way I try to import the package is:
from .utils.utils import *
Is there anything I am doing wrong?
I had written a utility script which I wanted to use for rest of my scripts. This is how I import -
sys.path.append(path_to_script)
import my_util as mu
df = mu.get_data()
We need to add the path to sys.path and then we can access that as a module.
path_to_script is the absolute path of the directory where your script/files are.
If the script is in same directory then we can simply import it as -
import my_util as mu
df = mu.get_data()
In this case, we do not need to add the path to sys.path as well. It is simply like importing another file inside a python application.

How to load files from subfolder into the main package

I have a python project with the following structure
package/__init__.py
package/foo.py
package/subFolder/__init__.py
package/subFolder/myModule.py
I would like to be able to import myModule on the level of package
import package.myModule
I've tried to import the modules inside the package/__init__.py with
from .subfolder import *
with no success...
Unfortunately I can't change the folder structure.
If you want to keep the autocomplete working, you could generate the __init__.py and fill __all__ with appropriate symbols.
If I understand your situation correctly,
you have your project directory that contains your script and a sub-Folder that contains a file defining a module.
From your script in the project directory, to access your module, the following should work:
from subFolder.myModule import *
What I ended up with was to append all modules from the subFolder to the __all__ Attribute in subFolder.__init__.py with the following snippet:
import pathlib
file_path = pathlib.Path(__file__).parent.glob('*.py')
__all__ = [path.stem for path in file_path if '__' not in path.stem]
And in package/__init__.py:
from .generated_execution_layer import *
An ugly side effect of this is, that it broke autocompletion for this imported modules.

How to import Python file?

Sorry, this is definitely a duplicate, but I can't find the answer. I'm working in Python 3 and this is the structure of my app:
/home
common.py
australia/
new-south-wales/
fetch.py
I am in the home/ directory, running fetch.py. How can I import functions from common.py in that script?
I've set up fetch.py as follows:
from common import writeFile
But I get the following error:
File "australia/new-south-wales/fetch.py", line 8, in <module>
from common import writeFile
ModuleNotFoundError: No module named 'common'
If I just do python -c "from common import writeFile" I don't see an error.
Shouldn't the interpreter look in the current directory for modules?
before import your directories that need to be imported must have file __init__.py in that folder
#solution 1 (import in runtime)
To import a specific Python file at 'runtime' with a known name:
import os
import sys
script_dir = "/path/to/your/code/directory"
# Add the absolute directory path containing your
# module to the Python path
sys.path.append(os.path.abspath(script_dir))
import filename
#solution 2(add files to one of python libraries)
also as you have a common library for you can run
>>> import sys
>>> print sys.path
and see what directories you can put your code and use in every project.you can move your common package to one of this directories and treat it like a normal package.for example for common.py if you put it in one root directory of one of this directory you can import like import common
#solution 3(use relative import)
# from two parent above current directory import common
# every dot for one parent directory
from ... import common
and then go to parent directory and run
python -m home.australia.new-south-wales.fetch
From the description I'm assuming you're not running this as complete python package, just as separate files.
What you can do is use complete modules. This means adding empty __init__.py to directories with your code. You'll also have to change the name of new-south-wales to new_south_wales, since it needs to be a valid module name.
Assuming home is the name of your app, you should end up with:
home/
__init__.py
common.py
australia/
__init__.py
new_south_wales/
__init__.py
fetch.py
Next, you'll need a startup script for your app - this means either something simple like:
#!/usr/bin/env python
from australia.new_south_wales import fetch
fetch.your_main_function()
Or you can add a setup.py with a full package description. If you specify entry points and the script will be automatically created.
Now that you're starting your code in context of a package, your fetch.py can do:
from ..common import writeFile

weird python3 import issue, No module named <module>

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

Categories

Resources