Importing python packages within python packages (relative imports, __init__.py) - python-import

Let's say I have a directory like this
In main.py I have the code from main_package import func but I get an error saying
test/main_package/func.py", line 1, in <module>
from sub_package.aa import aaf1
ModuleNotFoundError: No module named 'sub_package'
The line from sub_pacakage.aa import aaf1 in main_package/func.py is causing the error. I can switch that line to an asbolute path (from main_package.sub_pacakage.aa import aaf1) and it works fine.
However, I want to not change the relative path to an absolute path.
I am assuming I have to modify main_package/__init__.py to achieve this but I am not sure how. How do I go about this?
I've tried adding from . import sub_package to main_package/__init__.py but it doesn't seem to be working.

The issue with absolute Python imports is that they are actually relative to any path defined in sys.path and also to your current working directory.
Thus, to make an import like from sub_package.aa work, you need to ensure that the parent folder of "sub_package" is in sys.path or is your current working directory.
The same goes for from main_package.sub_package.aa. You need to make sure that the parent folder of main_package is in sys.path or is your current working directory. So it makes a different how and from which directory you execute your Python code. You usually need to "cd" to the right directory first.
I've had similar problems in the past and thus I've created an experimental, new import library: ultraimport
It gives you more control over your import and lets you do file system based imports and therefore real relative imports. You could then write your func.py like this:
import ultraimport
# Given that aaf1 is defined in aa.py from your sub package
aaf1 = ultraimport('__dir__/subpackage/aa.py', 'aaf1')
This will always work, no matter what is your current working directory, what is your sys.path or how you run your code.

Related

Playwright resolve relative path outside test directory

I have this problematic import.
import * as Collection from '../../lib/collection.js'
root
> lib
>> collection.js
> tests
>> folder1
>>> collection.spec.js
how can resolve this relative path with something like
import * as Collection from '#/lib/collection.js'
OR
import * as Collection from 'lib/collection.js'
tried both. neither worked.
any ideas?
I’m not sure what you mean when you say “problematic”, as that import looks correct/like it should work fine. Is it not working for you?
If your concern is that you’d rather avoid having to go up directories with ../ leading to the two options you tried, and you’re trying to get something like that to work, that’s addressed better than I could elsewhere such as this accepted answer about what # does and the others. Short answers:
lib/collection.js isn’t relative, and would refer to a package (see also Node’s doc on import specifiers
#/lib/collection.js isn’t a natively supported JavaScript syntax and requires a module loader/bundler, but should work once set up.

Agda Installation PLFA Configuration

I am trying to use the Programming Language Foundation with Agda plfa library, however the import does not appear to be working properly.
I have cloned the repository and added the repository path to: ~/.agda/libraries and plfa to ~/.agda/defaults.
When I create a test.agda file and check a single line
module plfa.part1.Naturals where
I get an import error:
You tried to load /Users/johngfisher/Desktop/agda_test/nats.agda
which defines the module plfa.part1.Naturals. However, according to
the include path this module should be defined in
/Users/johngfisher/agda_env/libraries/plfa/src/plfa/part1/Naturals.lagda.md
The file is present in the location the import is coming from so I am unsure of why Agda is unable to find it. Any help would be appreciated.
module plfa.part1.Naturals where
defines a module named plfa.part1.Naturals
Did you mean to type
module test where
open import plfa.part1.Naturals
instead?

How to check object class type in Dart

In my code, there is a place where I need to take different actions based on the input class type.
So I write two lines to check an input object's class type.
debugPrint("Let me know the next action: $action");
debugPrint((action is LoadPomodorosAction).toString());
And the output is
I/flutter (24128): Let me know the next action: Instance of 'LoadPomodorosAction'
I/flutter (24128): false
What does this mean?
The object 'action' is "Instance of 'LoadPomodorosAction'" and at the same time its class type is not LoadPomodorosAction .
how do I adjust my code so that I can know the class type of action?
I was suspecting that maybe there is something wrong with runtimetype. But how do I get to know the runtimetype?
I've tried replicating your issue and I'm not able to reproduce it. But to explain your inquiry, here is a complete details about the difference between the relative path and absolute path when used in imports as discussed in this SO post:
package imports
'package:... imports work from everywhere to import files from
lib/*.
relative imports
Relative imports are always relative to the importing file. If
lib/model/test.dart imports 'example.dart', it imports
lib/model/example.dart.
If you want to import test/model_tests/fixture.dart from any file
within test/*, you can only use relative imports because package
imports always assume lib/.
This also applies for all other non-lib/ top-level directories like
drive_test/, example/, tool/, ...
lib/main.dart
There is currently a known issue with entry-point files in lib/*
like lib/main.dart in Flutter.
https://github.com/dart-lang/sdk/issues/33076
Dart always assumed entry-point files to be in other top-level
directories then lib/ (like bin/, web/, tool/, example/,
...). Flutter broke this assumption. Therefore you currently must not
use relative imports in entry-point files inside lib/
See also
How to reference another file in Dart?
Previously, this bug was posted in GitHub as an issue between relative and absolute path. It seems that this was resolved per this GitHub post.

require a package that has a different file name in lua

I am very new to Lua, so pardon for the retarded question.
Following this tutorial I am trying to import DeepMind dqn into an iTorch notebook.
I have cloned this repo that contains a folder called dqn where the source code lives.
I have added the path to the dqn folder
package.path = package.path .. ";/path/to/dqn/?.lua"
When I try to do
require 'dqn'
I obtain an (expected) error, since there is no file called dqn.lua in the folder. The source code of such module is, in fact, contained in the file NeuralQLearner.lua.
I have seen the documentation that hints at the problem between file and package name.
So my question is: how can I import this module correctly? How can I let Lua know that dqn it should actually look for NeuralQLearner.lua (without hardcoding into the path I guess)?
The trick is in:
if not dqn then
require 'initenv'
end
put in every file. The initenv file will then define what dqn is supposed to mean. Once the path to initenv is package.path, this seems a nice trick to overcome the limitation.

Changing the Gridpanel class in Gridworld

I am trying to follow the steps to change the Gridworld appearance according to this. I've already imported source code for my gridworld jar file; ie. I can go and look at Bug.class or Gridpanel.class if I wanted to. However, I can't edit these files to produce the results that that pdf suggests. How do I do this? Did I import the source code incorrectly?
I don't think you can edit an external resource like gridworld.jar. You are going to need to create 4 new packages:
actor
grid
gui
world
Then copy the class files from gridworld.jar packages into your corresponding ones. Now you can edit the files. Make sure you include any miscellaneous files that might also be in the gridworld.jar packages, their important.
With your own packages you no longer need the gridworld.jar external resource, and there is a difference in your import statements. Instead of
import gridworld.actor.Actor;
Do this:
import actor.Actor;
Note: You will need to change all of the import statements in the new package classes to reference the other new packages. I believe that there were also some errors, not project breaking, but I just added suppressors.

Resources