How do I import module from directory in python,linux - python-3.x

I am running a python script however I get the following error;
ImportError: No module named 'config.paths'; 'config' is not a package
My project folder looks like this
Hell/
hen/
config/
paths.py
abc/
def/
code.py
and this project is the directory of my ubuntu system :
/home/fisker/workspace/Hell
I am trying to execute code.py, but It should get paths.py from config folder. How do I do this?
I even tried using;
PYTHONPATH="/home/fisker/workspace/Hell/:$PYTHONPATH" python code.py
and
appending the following code in the beginning of code.py
import sys
sys.path.append('/home/fisker/workspace/Hell')

You need to transform config to a package in order to import it.
Just create a __init__.py file in config directory.
Example:
Hell/
hen/
config/
paths.py
init.py
In your code.py file you will need to:
from code import what_you_want

Related

Getting no-name-in-module on pylint when having duplicates names

My current folder structure is something like this:
folder/
utils.py
something.py
etc.py
In something.py I do some imports:
from folder.etc import x
import utils.some_module
utils is a package installed from pip that unfortunately has the same name as one of my files inside folder. The code works normally but for some reason pylint gives me this error:
folder/something.py:11: [import-error(pylint), None] Unable to import 'utils.some_module'
folder/something.py:11: [no-name-in-module(pylint), None] No name 'some_module' in module 'utils'
If I change the file name from utils.py to something else the error disappears but I don't want to change it. The structure of package utils is as follow:
utils/
__init__.py
some_module
Does anyone have any idea how to fix this problem without changing the file name?
Thanks

Importing Module issue (name of project and name of module are the same)

So I am having an issue trying to import some modules right now because the name of the project is same as the module name. At least thats what I think.
I am trying to run the test_aws_file.py main file. That file is trying to import my.test.utils.util_file, but it is getting this error:
ModuleNotFoundError: No module named 'my.test.aws', so seems like i can import up to 'my.test' then when i add .aws then it would cause the error
test_aws_file.py import coding:
sys.path.insert(1, os.path.abspath('.'))
print(sys.path)
from my.test.aws.utils.utils_file import UtilsFile
project name (i do have a init.py in all directories): my.test.aws
my/
test/
aws/
utils/
util_file.py
test_aws/
test_aws_file.py
you should have __init__.py files in your directories to make those packages, otherwise the import wont work.

Python3 test folder import problems

Trying to write tests in Python3 with the folder structure
package/
__init.py__
Foo.py
Bar.py
test/
__init.py__
test_Foo.py
Foo.py
import Bar
test_Foo.py
import Foo
Running python3 -m unittest results in No module named Foo
If I changed it to
from package import Foo
It will resolve, but in Foo.py it'll have the error No module named 'Bar'
If I change Foo.py to
from package import Bar
The test will run without error, but running python3 Foo.py will result in No module named 'package'
I've gone through various answers about this kind of problems, like this one
Running unittest with typical test directory structure
and this
Python3 import modules from folder to another folder
and none of them address this issue
your project root (and also the working directory!) need to be the parent of package.
Then every import will be in the form import package.module or from package import module.
if you run python packgae/Foo.py or python -m package.Foo it should work.
Also take a look at pypa - sample project to see the recommended Python project structure.

jupyter notebook cant import a file whose path is mentioned in an imported script residing in different directory

I have a directory structure like this:
project/
nb/
notebook.ipynb
python/
data/file.p
settings.py
main.py
I am loading file.p under settings.py by writing open("data/file.p","rb").
Then I would simply write from settings import * in main.py and it loads the file. But when I write the same in notebook.ipynb it does not load the file because the current directory changes to nb/. Is it possible to load this file in notebook.ipynb without changing the directory path inside settings.py ?
Python is a package and settings is a module. So try this.
In notebook.ipynb
from python import settings
If it is not able to recognize the path, then set PYTHONPATH to absolute path of project directory and run again.

Unable to import module from another package

I have a directory structure like this
conf
__init__.py
settings.py
abc.conf
def.conf
src
main.py
xyz.py
src I chose not to make a package but a regular folder.
I am trying to import the settings.py file in the main.py and executing the whole thing with the command python3 main.py
My import statement in main.py : import conf.settings
The error I'm getting is No module named conf.settings and I can't get my head around it.
Is python failing to recognize conf as a package? Can packages contain files other than .py files (.conf files in my case)
When importing python search current directory and the sys.path. Since your main.py is in src folder it cannot see the conf package folder. Luckily you could update sys.path at runtime.
root
conf
__init__.py
settings.py
src
main.py
So you could append sys.path from main.py before importing conf module. Try following:
# main.py
import os, sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
from conf import settings
...
The other way is to update PYTHONPATH directly and add path to your script root directory.

Resources