Import python module from another package in vs code - python

I have a script with the following structure
\-A (git repository 1)
\-B
somefile1.py
__init__.py
setup.py
\-C (git repository 2)
newfile1.py
__init__.py
I want to use somefile1.py in newfile1.py, but I can't find a way to import it.
Both these files are in different levels. I can't change the format of these files.
I added from ..A import B in newfile1.py
But it didn't work in VS code.Is there any way to import the somefile1.py file?

From what I've understood you have trided this:
from ..A.B import somefile1
That called Relative import, you can learn more about this here`
But in this case absolute import should work too:
from A.B import somefile1

Related

Export everything from Python module or import everything from module

This may be well written somewhere on the internet (even on SO) but I could not find it.
Suppose I have a Python project structure like the following
mymodule
|—-__init__.py
|—-a.py
|—-b.py
|—-c.py
main.py
Now, I want to import in main.py everything from mymodule, meaning I want to be able to do the following:
import mymodule
mymodule.a.something()
mymodule.b.something_else()
I do not want to do the following:
import mymodule.a
import mymodule.b
import mymodule.c
How can I achieve this? In JS ES6, you would create a file mymodule.js for example, in which you would import everything that you want to export, and then export it. Is there something similar possible in Python?
As previously mentioned, when importing a module you are essentially importing its __init__.py file.
The correct way to handle this is to import whatever you need inside mymodule/__init__.py, and then all those imports will be available to whoever imports mymodule.

In Google Colab Python Notebook, what's the cleanest way to import other .py files?

I am trying to avoid using sys.path.append...or any hacks and want to organise the folders. Given I have a notebook .ipynb file, how can I import .py files if I place them in a directory ?
If you place them in the same directory, you can import your module (.py) files easily with:
from my_module import * # This will import all functions
from my_module import foo # This will import only my_module.foo
foo()
or simply import the module
import my_module
my_module.foo()
Refer to this python documentation for more explanation about module.
Update on updated question
As per this answer, you can use the above approach if you upload your module. Just ensure your pwd is correct.
Better yet in the sample, you can define __init__.py inside your module folder (refer to section 6.4. Packages in the doc above) to do something like:
from folder.my_module import foo
foo()

Python - fails importing package

I have trouble importing package.
My file structure is like this:
filelib/
__init__.py
converters/
__init__.py
cmp2locus.py
modelmaker/
__init__.py
command_file.py
In module command_file.py I have a class named CommandFile which i want to call in the cmp2locus.py module.
I have tried the following in cmp2locus.py module:
import filelib.modelmaker.command_file
import modelmaker.command_file
from filelib.modelmaker.command_file import CommandFile
All these options return ImportError: No modules named ...
Appreciate any hint on solving this. I do not understand why this import does not work.
To perform these imports you have 3 options, I'll list them in the order I'd prefer. (For all of these options I will be assuming python 3)
Relative imports
Your file structure looks like a proper package file structure so this should work however anyone else trying this option should note that it requires you to be in a package; this won't work for some random script.
You'll also need to run the script doing the importing from outside the package, for example by importing it and running it from there rather than just running the cmp2locus.py script directly
Then you'll need to change your imports to be relative by using ..
So:
import filelib.modelmaker.command_file
becomes
from ..modelmaker import command_file
The .. refers to the parent folder (like the hidden file in file systems).
Also note you have to use the from import syntax because names starting with .. aren't valid identifiers in python. However you can of course import it as whatever you'd like using from import as.
See also the PEP
Absolute imports
If you place your package in site-packages (the directories returned by site.getsitepackages()) you will be able to use the format of imports that you were trying to use in the question. Note that this requires any users of your package to install it there too so this isn't ideal (although they probably would, relying on it is bad).
Modifying the python path
As Meera answered you can also directly modify the python path by using sys.
I dislike this option personally as it feels very 'hacky' but I've been told it can be useful as it gives you precise control of what you can import.
To import from another folder, you have to append that path of the folder to sys.path:
import sys
sys.path.append('path/filelib/modelmaker')
import command_file

No module named "data_utils"...But it is downloaded

So, I was having the simple error,
"No module named "data_utils"
when trying to import it into a python program. So I thought it must not have downloaded and spent like 20 mins trying to ensure a proper download. Turns out it was fine all along and the data_utils.py file is in the utils folder.
I'm really stuck because I see it right there, yet it simply won't import. I looked for a .bin after the __init__.py files but it seems like they are fine. Any help would be greatly appreciated. Thank you!
Deducing from your comment the answer would be:
The files have to be in the same directory /and some sub-directory for an import like import data_utils to work.
There are some way to get around this but to start try to keep it simple at first.
for example:
given a directory structure like this:
| --main.py
| --data_utils.py
| --train.py
and suppose you have a function remove_punctuation in data_utils:
you could use import that with:
from data_utils import remove_punctuation
or you could import all the functions (and or classes in data_utils) with:
from data_utils import *
or you can import data_utils with
import data_utils
# use remove_punctuation
data_utils.remove punctuation
The directory structure could also be:
|--main.py
|--bar
|--foo
|--src
|--train.py
and you could import remove_punctuation using
the same semantics as above qualifying the directory using dot notation:
from bar.foo import foo
EDIT: would module import like scipy have to be in the same directory?
Short answer
no
Long answer:
When you install scipy and numpy etc (packages you install from pip or using the sudo) they add themselves (their location) to the PYTHONPATH so you don't have to have them in the same directory as your project code.
Modules that you want to use globally must be added to your PYTHONPATH. Python searches for modules (roughly) in the directory and sub-directory of the file it is executing and in the PYTHONPATH.
if you want global import of your own modules
I.e, if you want to use data_utils.py everywhere you could do assuming you are using bash on linux and assuming you have data_utils.py in a directory named data-utils:
add an __init__.py to data-utils, so your directory structure would look like this:
|--data-utils
|--__init__.py
|--data_utils.py
then add this line to your ~/.bashrc:
export PYTYHONPATH=$PYTHONPATH:/path/to/data-utils
data-utils (and by extension data_utils.py) is permanently added to the PYTHONPATH and can be imported by any project code.

how to import py file from different upper folder?

All,
File structure:
\
util
utils.py
modules
__init__.py
modules1.py
submodule
__init__.py
submodule.py
I want to know how to import utils.py in those
__init__.py
for example, now I run the python interpreter in \ level, I run import modules, I suppose the code from ..util.utils import * may works, but it is not.
may I know where is the mistake?
and may I know if there's a way i can import utils.py in a universal format?
something like
import \util\utils.py
I know I may use path.append(), but any alternative?
Thanks
============
got the answer from this post:
Import a module from a relative path
If you are developping a python package (what you are obviously doing, as you have init.py), then the most simple way to import your module is just via the package.
For example, if your package is called mypackage, then:
import mypackage.utils

Categories

Resources