from .. import folder1 ImportError: attempted relative import with no known parent package - python-3.7

Following some tutorial online, I am trying to import a folder as a package into a file.
Here is the hierarchy of my project:
folder1
folder2/
file.py
__init__.py
So, in file.py I am trying to import the content of folder1 as a package:
#!/usr/bin/env python3
from .. import folder1
But when executing file.py
./file.py
I get:
Traceback (most recent call last): File "./file.py", line 9, in
from .. import folder1 ImportError: attempted relative import with no known parent package
Any solution ?
Thanks!

Not the best solution but you can run this command in your command line while in the parent directory of folder1 and folder 2:
python -m folder2.file
Else, you can add an __init__.py file in the directory containing both your folders:
folder1
folder2/
file.py
__init__.py
__init__.py
Last solution I can think of is:
import sys
sys.path.append(r"/path/to/your/project")
You will be able to import folder1 like so:
import folder1

Related

How to import the file in other project/directory by configuring the Makefile or _Coqproject in current project

I would like to import the file of one of my projects when coding the current project.
i.e. Import the file ../ProjectA/fileA.v in ./fileB.v.
How can I do this via configuring the Makefile or _Coqproject.
You add the following line at the beginning of the _CoqProject:
-R ../ProjectA SomeName
And then you should be able to write
From SomeName Require Import fileA.

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

Cython project structure with dependent extension classes

I'm getting to the point with a project where I need a proper directory structure. I'm trying to arrange this and getting ImportErrors when using my cython extension classes.
The directory structure looks like:
.
├── __init__.py
├── Makefile
├── README.rst
├── setup.py
├── src
│   ├── foo.pxd
│   ├── foo.pyx
│   ├── __init__.py
│   └── metafoo.pyx
└── test
├── test_foo.py
└── test_metafoo.py
The contents of all files can be found (in commit e635617 at time of writing) of this github repo.
My setup.py looks like the following:
from setuptools import setup, Extension, Command
from Cython.Build import cythonize
SRC_DIR = "src"
PACKAGES = [SRC_DIR]
ext_foo = Extension(SRC_DIR + ".foo",
[SRC_DIR + "/foo.pyx"]
)
ext_meta = Extension(SRC_DIR + ".metafoo",
[SRC_DIR + "/metafoo.pyx"]
)
EXTENSIONS = cythonize([ext_foo, ext_meta])
setup(
name = 'minimalcriminal',
packages=PACKAGES,
ext_modules=EXTENSIONS
)
The complexity seems to lie in that extension classes in metafoo.pyx use extension classes from foo.pyx.
After building with python setup.py build_ext --inplace, the test_foo.py program runs ok:
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
import src.foo as foo
somefoo = foo.Foo(2)
somefoo.special_print()
When run from both the cyproj/test and cyproj directories:
/cyproj$ python test/test_foo.py
The value of somefield is: 2
and
/cyproj/test$ python test_foo.py
The value of somefield is: 2
But the test_metafoo.py crashes when run in the cyproj/test directory:
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
import src.foo as foo
import src.metafoo as metafoo
lotsafoo = [foo.Foo(i) for i in range(4)]
mf = metafoo.MetaFoo(lotsafoo)
mf.special_print()
With the message:
ubuntu#ubuntu-UX21E:/projects/cyproj/test$ python test_metafoo.py
Traceback (most recent call last):
File "test_metafoo.py", line 6, in <module>
import src.metafoo as metafoo
File "cyproj/src/foo.pxd", line 6, in init cyproj.src.metafoo (src/metafoo.c:1154)
ImportError: No module named cyproj.src.foo
But runs properly from the parent cyproj directory:
/cyproj$ python test/test_metafoo.py
The value of somefield is: 0
The value of somefield is: 1
The value of somefield is: 2
The value of somefield is: 3
I don't really get what's driving the different behaviour of these errors. If I can't use import src.foo in test_metafoo.py why does it work in test_foo.py?
Similarly if I open up an interactive session in the parent directory and try to import all:
In [1]: from src import *
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-7b8bc2c1dfb9> in <module>()
----> 1 from src import *
/projects/cyproj/cyproj/src/foo.pxd in init cyproj.src.metafoo (src/metafoo.c:1154)()
ImportError: No module named cyproj.src.foo
When src/__init__.py looks like:
__all__ = ["foo", "metafoo"]
Which I thought would allow importing all...
I was able to compile and test your package after removing the __init__.py file from the project root directory and changing test_foo.py and test_metafoo.py.
sys.path.append(os.path.abspath("."))
sys.path.append(os.path.abspath("../"))

Importing module from another folder. Python 3.3

Pretty new to python. Running linux os
I have a folder folder1 with my main program "main.py"
I have a folder folder2 with my module "module.py"
The module has 2 functions in it function1 and function2
Both folders are in the same folder.
How do I import the module.py from folder2 into my main.py in folder1? or do I have to pick out the individual function from module.py somehow?
preferably just import both functions at once
You can do:
import sys, os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
+ '/folder2/')
Then you can import as usual. Please see the answers to a similar question.

How to import python modules from parent and sibling packages

This (or similar) question has been asked many times before, but none of the solutions offered work in my case.
My project structure is like this :
| project_2
main.py
__init__.py
systems.py
| config
__init__.py
options.py
| database
__init__.py
database.py
entity.py
| tests
__init__.py
test_systems.py
test_options.py
test_database.py
test_entity.py
Obviously I need to import all the modules in the test modules under the tests package. I tried relative imports with the dot syntax:
from ..systems import System
from ..config import options
from ..database.entity import Entity
Returns a ValueError: Attempt relative import in non-package. I have tried that with a package structure where everything (including systems) is in its own package. It fails with the same message.
What really bothers me is that this is supposed to work: PEP 328, but it does not. I really want to avoid having to append the packages to $PYTHONPATH or to use some insane method such as loading the modules with imp from the file path.
I read that part of the problem might be that systems.py is in the main package, but that does not explain why the rest of the relative imports do not work either.
P.S. I actually recreated the example from PEP 328 just to test it and it does not work.
You get that when a python file does a relative import, but that file not loaded as a module via import in another module (but e.g. from the commandline). Given this structure:
.
├── main.py
└── test
├── __init__.py
├── a.py
└── b.py
main.py:
from test.a import A
print A
a.py:
from .b import B
A = B
if __name__ == '__main__':
print A
b.py:
B = 'b'
Now try:
python main.py
result is
b
and with
python test/a.py
you get:
Traceback (most recent call last):
File "test/a.py", line 1, in <module>
from .b import B
ValueError: Attempted relative import in non-package
What does work is:
python -m test.a
If you simply add . to your python path, if you run the script from the project_2 folder relative paths such as config.options will work. This requires an update to PYTHONPATH on every machine, unfortunately.
Tested on Python 2.7.14