How to import module up multiple levels from base? - pytest

I'm trying to call a module several levels up from pytest run directory. How do I import this module?
I've tried:
sys.path.insert(0, 'path/to/module')
import modulename
Here's the directory structure:
tools
common
modulename.py (contains class A)
functional_test (this is where I'm running pytest; tools/functional_test)
conftest.py
pytest.ini
tests
typea (this is tools/funtional_tests/tests/typea)
tests_typea1.py
Under tests_typea1.py, I want to import class A from modulename.py under tools/common.
Getting the following Errors:
ImportError
ModuleNotFoundError: No module named 'modulename'

Well, after having another person look over my code, I realized that I miss-spelled my the import . Ugh... Note to self: change font style and confirm spelling. :). The above sys.path.insert or sys.path.append with import <modulename> works.

Related

import statsmodels package returns ModuleNotFound despite correct path

I am trying to import statsmodels.stats.weightstats.DescrStatsW.tconfint_mean.
I run
from statsmodels.stats.weightstats.DescrStatsW import tconfint_mean
I get the error
ModuleNotFoundError: No module named
'statsmodels.stats.weightstats.DescrStatsW';
'statsmodels.stats.weightstats' is not a package
I have confirmed that I can import other packages from statsmodels with no problems.
I must be using the wrong path, but the docs don't specify any other path to use.
I just figured it out. I will leave this up for progeny.
from statsmodels.stats.weightstats import DescrStatsW
DescrStatsW.tconfint_mean(...)
Not sure why this behaves differently from other python libraries.

ModuleNotFoundError: No module named 'myfile.py'

So I have trawled through other very similar issues and tried many variations but I cannot get this to work.
I have created code in a separate file under the following structure:
/somefolder/ #this is where my main code resides
/somefolder/src/ this is where my myfile.py resides that I want to import.
My main code starts as follows:
import os.path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../src'))
import myfile as mine
but then I get teh following error:
ModuleNotFoundError: No module named 'myfile'
Any assistance would be very welcome.
Assuming your current directory is PythonApplication1, you can follow these steps to access myfile module.
create __init__.py file in both PythonApplication1 and
PythonApplication1\src directories
Now import myfile as from src import myfile as mine

Julia: how to import a module

I'm working with the language Julia and my IDE is juno.
Now I want to import my own module. Here is an example:
First I create a module file:
module my_module
export test
function test(id, name)
print("Your ID:", id, ". Your name: ", name)
end
end
Its path is: C:\doc\my_module.jl
Now I want to import my_module.jl into another julia project. Here is the code:
import "C:\doc\my_module.jl"
It doesn't work and I got an error:
invalid "import" statement: expected identifier
What should I do?
To import a module, you need to include the file then import the module.
See the comment of #Gnimuc Key
There are still other ways to import a module.

Python module function not defined

I am trying to import a module in my python script and I can't make it work.
So I have my python script: /home/user/pythonscript/oneDir/onescript.py
And I would like to use a script that is a directory higher in hierarchy:
/home/user/pythonscript/common.py
So I did the following at the top of my onescript.py:
import sys
sys.path.insert(1,'/home/user/pythonscript')
import common
In my common.py file, I have a function onecConnect, and when I try to run onescript.py, which uses onecConnect function, I get the following error: nameError: name 'onecConnect' is not defined
Anyone can see what I do wrong or forgot to do?
Thanks
Make sure there are __init__.py in all directories, go to /home/user/pythonscript and run Python code from there. So:
python oneDir/onescript.py
In onescript.py you can do:
from common import onecConnect
The rules are:
Always run a Python script from the highest possible directory (not the deepest into the project).
Always have full import lines, no relative imports.
This keeps the problems away.

Why does importing from a module from the current directory only work when within that directory?

Background
I have a Python project with this directory structure:
py/:
db/ __init__.py run.py
py/db:
handle.py __init__.py util.py
The files are simple enough that I'm not sure I need to post them; nevertheless:
py/run.py
from db.handle import Handle
py/db/handle.py:
import util
class Handle:
def __init__(self, x):
self.x = util.addtwo(x)
py/db/util.py:
def addtwo(x):
return x + 2
If I run handle.py from within the db subdirectory, it imports util without error. However, when I run run.py, handle.py fails with an import error. I can guess that handle.py is being run in the py directory (instead of py/db), and putting a call to os.getcwd() in handle.py confirms this. I can fix this problem using sys.path like so (in run.py):
import sys
sys.path.append("db")
from db.handle import Handle
Question
When importing, from a subdirectory, a module that contains imports to other local modules in that directory, why doesn't Python check the current directory of the module making the import statement? In my example, why doesn't Python check the db first when handle.py contains import statements? Is there a PEP that describes this or is it a behavior with an obvious rationale that I missed?
I thought it might be related to PEP 328:
all import statements be absolute by default (searching sys.path only) with special syntax (leading dots) for accessing package-relative imports.
but I'm not sure.
Your import is "absolute" and module names is looked for in the PYTHONPATH, and that typically includes the current directory.
If you want to import a module from the same folder that your module is in, you use a relative import:
from . import util
or
from .util import addtwo