ModuleNotFoundError seen from module being imported from different folder on module being imported from same folder - python-import

Folks, sorry for the convoluted question title
I have the following dir structure:
projectfolder/source
- main.py
- module1.py
- module2.py
projectfolder/tests
- test-module1.py
- test-module2.py
module2.py imports module1.py, test-module1.py works just fine, I added
import sys
sys.path.append("..")
in test-module1.py and imported source.module1
but when I try to run test-module2.py(with the same import sys and sys.path.append(...)) I get
File "../source/module2.py", line 1, in
from module1 import *
ModuleNotFoundError: No module named 'module1'
What am I doing wrong? Also this is my first python project I am building from scratch, what is the recommended dir structure? should main be in the projectfolder root?
This is python3 so there is no need for init.py I think..

Related

import fuction from package to another package

can you please tell me how can I import a function from poly.py to query.py
this is the directories tree :
my_project
|_dataset
|_execute
|__poly.py
|_search
|__query.py
|_determine
|_optimize
|_transmit
NB : my python version is 3.6.2 > 3.3 so _init.py is unuseful
You need put those files in 1 folder and do import filename or from filename import function

Could not find module ‘Text.Megaparsec.Char.Lexer’

I tried "stack install megaparsec", adding megaparsec and parsec as a dependency in the project.yaml file and running stack build, i tried stack build on all levels of the project folder. Also what's weird is that I can load the module which contains the imports which are marked as problems by VSCode and show the error message and I can also run the functions in said module which make us of the imported stuff but I can't compile the file.
ghci version is 9.0.1
stack version is 2.7.3
cabal-version: 1.12
Error message from terminal
module Parsing where
import Control.Monad (void)
import Data.Void
import Syntax
import Data.Text (Text)
vvvvv these 3 are marked as problems and are the reason I can't compile the file.
import Text.Megaparsec
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
.cabal file
library
exposed-modules:
Example
Lib
Parsing
Print
Semantics
Syntax
other-modules:
Paths_parser
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
, containers
, megaparsec
, parsec
, text
default-language: Haskell2010
executable parser-exe
main-is: Main.hs
other-modules:
Paths_parser
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, parser
default-language: Haskell2010
test-suite parser-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_parser
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, parser
default-language: Haskell2010

ModuleNotFoundError: No module named 'pyproj._network'

I am trying to run Python Code on Spyder for scientific computing. I use geometric information from shape files using geopandas. Installing geopandas was already difficult but I finally managed. However now I have another issue, that a module called 'pyproj._network' is not found.
I checked and the module is there and was also added to the path using the PYTHONPATH manager of Spyder.
Now I dont know anymore- any help very much appreciated!
Thanks!
`ModuleNotFoundError: No module named 'pyproj._network'
Entering post mortem debugging...
c:\users\stefano-work\miniconda3\lib\site-packages\pyproj\network.py(10)()
8 import certifi
9
---> 10 from pyproj._network import ( # noqa: F401
11 _set_ca_bundle_path,
12 is_network_enabled,
`

ImportError during python run

Currently I am executing doamusic python codes from
https://github.com/yump/doamusic.git
I am using python 3.7.2. (Python IDLE) on windows.
My directory tree is
F:\doamusic_project\doamusic\music.py
when I run music.py, I am getting the following error
Traceback (most recent call last):
File "F:\doamusic_project\doamusic\music.py", line 31, in
from . import util
File "..\doamusic__init__.py", line 1, in
from doamusic.music import *
File "..\doamusic\music.py", line 32, in
from . import _music
ImportError: cannot import name '_music' from 'doamusic' (..\doamusic__init__.py)
What is the reason for not importing _music ?.
You are trying to do an import from .pyx file. A .pyx file must be compiled unlike a .py file.
Try to do that before the import of _music:
import pyximport
pyximport.install()
More info Cython

Flask vs Terminal: how do I manage imports so that I can use codes in both?

I have a Flask project with the following directory structure:
-my_project
- flask_code.py
- module
- submodule
- foo.py
- bar.py
- xyz.py
In flask_code.py, I must import both foo and bar. Therefore, flask_code has the following import statements:
"""flask_code.py"""
from module.foo import *
from module.bar import *
In foo.py, I need to import xyz. I also have implemented a main function in foo for testing, which I usually call from command line. If I write the imports of foo like this:
"""foo.py import 1"""
from module.xyz import *
The flask application runs exactly as expected. But, if I try to run foo.py from command line, I get the following error:
ModuleNotFoundError: No module named 'module'
On the other hand, if I change the imports of foo to:
"""foo.py import 2"""
from xyz import *
the ModuleNotFoundError is not thrown anymore when foo.py is called from command line, which I guess it's expected. But, the flask application can't find module xyz on its own.
Both my_project and module folders have a init.py empty file.
I want to manage these imports so that I can both use the flask application and call the scripts from command line for testing. Thanks in advance!

Resources