Python: ImportError happens on IDE suggestion - python

This is my packages structure:
This is my __init.py__ inside settings package:
from settings import *
This is my functions.py:
from git import *
import initializer.settings as settings
_repo_remote = "https://%s:%s#%s" % (settings.git_username, settings.git_password, git_info["remote"])
Although I imported the settings package with my IDE auto-complete, I keep getting:
ImportError: No module named initializer.settings
When changing my import to:
import settings
The code works, but IDE is showing an error, why does it happen and what's wrong? I assume it is something with the path it try to load the module from, but I don't know how to change or control it..

If your main.py is in the 'initializer' folder (it appears to be?), you could import simply like this instead:
import settings
As long as the __init__.py in the 'settings' folder is like you described it. You would need to have the 'main.py' in the topmost folder to use it as you had it.

Related

with __init__.py import start from root, why still No mudule in subfolder Python 3

I want to import .py in a subfolder from a .py in another subfolder. I have learned
you should add init.py in subfolder and you should include your root path
here is my forlder structure :
pycharm_project_973
__init__.py
/slic
multi_slic.py
__init__.py
/muilti
main_unet_up4.py
__init__.py
here is some import code I tried in main_unet_up4.py :
from pycharm_project_973.slic.multi_slic import multi_slic as multi_slic
from pycharm_project_973.slic.multi_slic import DataGenerator as DG
import pycharm_project_973
each time and I got :
ModuleNotFoundError: No module named 'pycharm_project_973'
then I tried:
import slic
ModuleNotFoundError: No module named 'slic'
from slic.multi_slic import DataGenerator as DG
ModuleNotFoundError: No module named 'slic'
import slic.multi_slic
ModuleNotFoundError: No module named 'slic'
I am using anaconda virtual environment and tmux. This folder is a pycharm promote project location in server. I use mark directory as source root in pycharm and it worked well then I tried to work on server it remind me this No module issue.
Please help, thanks!
edit after the comment I tried to use:
from ..model import multi_net
I got
ValueError: attempted relative import beyond top-level package
sys.path.append("..")
from model import multi_unet
It worked well.
I guess it is because that the root dir has been added to my system lib search list.

VSCode Unable to import 'example' pylint(import-error)

I am getting pylint errors in VSCode that say they are unable to import local files. However, I am able to run the files through the debugger with no problem. I thought that pylint used the same PYTHONPATH that the interpreter uses, so I don't know why this is happening.
I have my code set up like so:
dir0
-dir1
--__init__.py
--src
---__init__.py
---srcdir1
----__init__.py
----file1.py
---srcdir2
----__init__.py
----file2.py
file1.py looks like this:
def func1():
return 1
file2.py looks like this:
from srcdir1.file1 import func1
func1()
in launch.json I have:
"env": {"PYTHONPATH": "/full/path/to/dir0/dir1/src:/usr/local/bin/python"}
Pylint is giving me an import error around "from srcdir1.file1". When I go into the debugger and click run debugger, the file runs with no issues. However, if I right click and select Run Code, I get import errors that match the pylint errors.
EDIT:
I created a file in my workspace folder called .env in my workspace folder. It is as follows:
PYTHONPATH=/Library/Python/2.7/site-packages:/Users/user/path/dir0/dir1/src:/Users/user/path/client/src:/Users/user/path/product/src
Interestingly, I can import from product (the third in the list) but not from client. Is there somewhere that this environment is being overridden?
I also have the following in the file:
import os
import shutil
import sys
For some reason, import sys (but not the others) gives me the following error: unresolved import 'sys'Python(unresolved-import)
Do you have __init__.py files inside those folders? Otherwise python won't recognise them as modules and will be unable to import the code. Have a look at https://stackoverflow.com/a/448279/5015356 for more information
The problem is that you specified a PYTHONPATH for the debugger and not the general extension to send to Pylint. Try setting PYTHONPATH in a .env environment variable definition file.

Cannot find module when importing from project in 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

Can't import module from sibling directory

I have a Python 3 project that's structured like this:
/project
__init__.py
/models
__init__.py
my_model.py
base_model.py
/tests
__init__.py
test.py
In test.py I want to import my_model. My first attempt was from models import my_model, which threw an ImportError: No module named 'models'. This question recommended adding an __init__.py file to each directory, which didn't help. Another post said to modify the path with:
import sys; import os
sys.path.insert(0, os.path.abspath('..'))
but this throws an error when my_model tries to import from base_model.
This seems really straightforward but I'm stumped. Does anyone have any ideas?
Adding the sibling directory to sys.path should work:
import sys, os
sys.path.insert(0, os.path.abspath('../models'))
import my_model
Use absolute imports everywhere: from project.models import my_model, should work fine from wherever in your project, no need to mess with paths either.
The answer depends on how you launch test.py.
The only way I know to do relative imports is to have the file in a package. For the Python interpreter to know you're in a package is to import it in some way.
Use:
from ..models import my_model
in test.py
And launch the Python Interpreter below the project folder.
You will then be able to import project.tests.test without error.

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