getting ModuleNotFoundError: No module named <user defined library> - python-3.x

I have the following folder structure and a user created file name paths.py that has functions to deal with path related operations
project_home:
-paths.py
project_1:
main.py
project_2:
main.py
etc
when I try to import paths in main.py in the subfolders it say, it works only if I call the file from the project home only ...
error :
import paths
ModuleNotFoundError: No module named 'paths'
any idea on what I could do to import files from home directory ... usually if the files are under subfolders we can call them using "Import subfolder.file" since its in main working directory the command Import paths is failing is what I am feeling ...

Related

How to import a module from parent directory within a subdirectory

I was working on a simple project, bike_rentals. I thought all was well until I began writing test files for the project.
I created a new directory and name it TestBikeRental and saved it within the BikeRentals directory, which contained the scripts for my project. I created the file init.py within the BikeRentals and TestBikeRentals directory to make them each be a package. Within the top directory is the script run_script.py that I use to run the modules\scripts contained in the package BikeRentals.
Directory structure
The problem comes up when I try to import the py script db_connection (underlined in yellow), within the Test_bike_rentals.py.
This is how tried to imported db_connection.
Importing db_connection
After numerous and numerous attempts, the errors that I kept receiving were these two:
from BikeRentals.BikeRentals.db_connection import Connect
ModuleNotFoundError: No module named 'BikeRentals'
from..db connection import Connect
ImportError: attempted relative import with no known parent package
To view the sourcecode in github repo:
https://github.com/Brownred/Python-and-SQL
I tried to import a module but got an error after numerous attempts. I was expecting no error when it comes to importing a module.

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.

How do I import a file to execute a function

I have a file with a function. I want to import the file and when doing that I get No module named ex25.
import ex25
I have checked some tutorials and the offial documentation.
import ex25
No module named ex25
First, check if the file is .py file and in the same folder with the file that you are currently working on, there shouldn't be any problem at all.
If not in the same folder, but in a different folder and make sure that your file is .py file, you need to make that folder (which contains the file) to a package by adding a file name __init__.py

How do I import module from directory in python,linux

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

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.

Resources