How to import a module from parent directory within a subdirectory - python-3.x

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.

Related

getting ModuleNotFoundError: No module named <user defined library>

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 ...

ModuleNotFoundError: No module named 'app'

Whenever I run this script under the folder app/klarna_basic/klarna_basic_test.py, I always get the error:
ModuleNotFoundError: No module named 'app'.
The code used for importing is,
from app.Fortnox import Fortnox
Fortnox.py is located in app(app/Fortnox.py) folder.
See the image below.
What seems to be problem on this one?
it looks like Fortnox.py is inside the subfolder klarna_basic
from klarna_basic.Fortnox import Fortnox
orimport klarna_basic.Fortnox

Python sub folder modules not able to import other subfolder modules

I have been struggling to figure out how to organize my code effectively with many modules of various types. I have a three layered folder system. The parent directory contains the main.py file which imports and runs the main code. The modules are in a subfolder called lib, and different modules are placed in further subfolders. However, when I import one module from a subfolder, that module itself fails to import modules in the same sub directory. Sorry if this is a bad question, but I cannot figure out why the import statement isn't working, and I have looked at google and stack overflow and have not found any similar problem. All advice is welcome.
Parent Directory
---->lib
|--->module_group
|module_one.py(that tries but fails to import module_one)
|module_two.py
|main.py
In main I run
from lib.module_group.module_one import Module_One
Which works until an error is handled saying that there is no such module as module two. However, when I run module_one by itself, it works fine with the following import statement.
from module_two import Module_Two
Set PYTHONPATH to the directory you want to act as your root. For example, if you want to use the following code:
from lib.module_group.module_one import Module_One
Then set PYTHONPATH to the directorying containing lib. For example:
lib/module_group/module_one.py:
from lib.module_group.module_two import Module_Two
lib/module_group/module_two.py:
class Module_Two:
print('Loaded Module_Two')
Then, to run module_one.py directly and still enable it to use lib.module_group.module_two to load Module_Two, use something like:
$ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
Loaded Module_Two

Meteor Shell, can't import files from any folder: Error: Cannot find module ‘/imports/api/donuts/collection.js’

I am having some trouble importing modules into the meteor shell.
Simple example:
1.create new project (meteor create myproject)
2.create file /imports/api/donuts/collection.js and paste content:
// file: /imports/api/donuts/collection.js
import { Mongo } from 'meteor/mongo';
const Donuts = new Mongo.Collection('donuts');
export default Donuts;
3.Run meteor shell and import the file by:
import Donuts from '/imports/api/donuts/collection.js'
than this error hits up:
Error: Cannot find module '/imports/api/donuts/collection.js'
at Function.require.resolve (packages/modules-runtime.js:129:19)
at Module.resolve (packages/modules-runtime.js:81:25)
at Module.Mp.import (/home/ec2-user/.meteor/packages/modules/.0.7.7.mccaq7++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:61:29)
at repl:1:-37
at packages/shell-server/shell-server.js:458:25
at /home/ec2-user/.meteor/packages/promise/.0.8.8.i94065++os+web.browser+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:32:39
What's wrong? File permissions are ok, I start the meteor shell from project root.
Thanks!
Meteor originally loaded all of the source files using its default load order.
In more recent versions (circa v1.3), it treats special directories differently. One of those directories is imports.
Any directory named imports/ is not loaded anywhere and files must be imported using import.
(from the Meteor docs)
When using the shell, you can only import resources that were included in the build. If the module (file) you are trying to import is not included in your import tree (chain of imports starting somewhere outside of the /imports directory), it will not be available for import.

No module named error

I encountered an error, while i am practicing Login with authentication-pyramid framework - "init.py" as below,
File __init__.py, line 4, in <module>
from .security import groupfinder
ImportError: No module named 'myproject.security'
I have placed security.py file inside myproject folder. but
when I changed import as,
from security import groupfinder
web app successfully runs.
My question is why .security throws error as "No module named 'myproject.security" while security.py is still inside myproject folder. Is that ".security" and "security" is different. What that operator "." corresponds to?
When importing files, Python uses the . notation to indicate submodules. In this case, since your two files are in the same folder (and since you are running inside the myproject namespace), there is no need to indicate a subdomain.
If you had two modules, myproject and myproject2, and wanted to import a file from myproject2 into myproject, then you would need to use this notation.
In all cases, however, importing .anything without being preceded with a module, such as myproject2.anything will cause an error. Hopefully this helps, I'm not great at explaining this stuff.

Resources