How to import modules from py files - python

So I received three python files with some functions. It's an util library provided by a third party. When I want to import these in my python file I use this code (as was specified by the third party):
import util.submission as S
import util.vis as V
import util.metrics as M
But then I get this error. I am working in Google Colaboraty and I uploaded the three python files in my current working directory on google colab so I don't understand. How do I fix this? The three files are just named submission.py ; vis.py and metrics.py. Any help here? This should be quite basic but it doesn't seem to work.
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-8-1e1451b5bba0> in <module>()
1
----> 2 import util.submission as S
3 import util.vis as V
4 import util.metrics as M
ModuleNotFoundError: No module named 'util'

Create a folder in your current working directory named utils, and place the three .py files inside of it. You can then use the import statements as you have specified.
https://realpython.com/absolute-vs-relative-python-imports/#syntax-and-practical-examples

Related

ImportError: cannot import name 'weather_api_key' from 'config'

I am trying to import an API key from a python file and I get this error:
ImportError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_27940\425884591.py in
9
10 # Import the OpenWeatherMap's API key
---> 11 from config import weather_api_key
ImportError: cannot import name 'weather_api_key' from 'config' (C:\Users\mjrm_\anaconda3\envs\PythonData\lib\site-packages\config_init_.py)
I have done many fixes that I found but I keep getting this error.
I tried changing the py file name and also attempted verifiy loading different packages into my environment, I feel like the error fix is simple but I cannot figure this out

Cannot load .pyd module in Python

I am writing a workflow in Jupyter that uses some C code which has already been compiled to a .so file (for Linux, where everything works fine) and a .pyd file (for Windows, which I am using). The import tree is a little complicated, so bear with me. The error message hopefully explains it:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-9-cabd8b5ab266> in <module>
12 import plotly.graph_objects as go
13
---> 14 from lase_analysis import load_lasefile, load_timeseries
C:/Users/Owner/.../v2\lase_analysis\__init__.py in <module>
2 from .spectrum import Spectrum
3 from .threshold import Threshold
----> 4 from .lasemap import LaseMap
5 from .laseseries import LaseSeries
6 from .readermatl import ReaderMATL
C:/Users/Owner/.../v2\lase_analysis\lasemap.py in <module>
14 from .filelase import FileLase
15 from .spectrum import Spectrum, PeakFitOpt
---> 16 from .utils.c_funcs import clst_dist
17
18
ImportError: DLL load failed while importing c_funcs: The specified module could not be found.
Basically, in the Jupyter session, I am importing __init__.py from the folder lase_analysis (the first arrow in the error message), which imports a class LaseMap from the file lasemap.py. Then, this lasemap.py file tries to open a C function clst_dist from the file c_funcs.pyd, which is located in a folder called utils which is in this same folder lase_analysis that all of the previously mentioned Python files are located in. However, I still get the error that the .dll file (which should be equivalent to a .pyd file here) cannot be found.
Any help would be appreciated, and my apologies if this was a bit hard to follow.

How can I import 2 module in Python?

Guys I'm new in Python and I'm trying to understand modules.I have a folder in desktop and there are 2 module in that folder. One of them is pr.py ,it's takes a array and displays it.And other one is fillArray.py.Now I want to add these 2 friend into interpreter but when I used in interpreter import pr.py or import fillArray it's giving to me this error
>>> import pr
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pr
ImportError: No module named pr
Then if I clicked f5(run) in pr.py module ,write again in interpreter import pr it works.That's ok.But trying to run fillArray.py module same steps, it's restart interpreter and it works butpr.py module is removing.How can I handle this?By the way this can be unnecessary but I'm using python2.7.
Edit:I wrote print.py sorry it should be pr.py
If you have both the modules in the same folder, you can use . which specifies current directory as follows:
from .pr import *
This statement will import all functions of print.py which is in the current directory
If you want specific things to be imported, just specify using their names
from .pr import myfunc1, myfunc2, myclass1, myclass2
If you wish to import the whole module, use:
from . import pr
Hope this helps :)

package on sys.path, with init importing names, can be used internally, but not from outside it

I have a package my_scripting_library I want to use anywhere on machine. It has an init.py:
from silo_functions import *
and looks like
my_scripting_library
-__init__.py
-silo_functions.py
-test_stuff.py
test_stuff.py looks like:
#!/usr/bin/env python
from silo_functions import *
lines = read_lines('filepath.py')
print lines
in bashrc:
export PYTHONPATH="${PYTHONPATH}:$LIBRARY"
where LIBRARY is a correct filepath to my_scripting_library
In [1]: import sys
In [2]: sys.path
Out[2]:
['',
'/usr/bin',
'/usr/lib/python2.7/site-packages/lxml-3.3.3-py2.7-linux-x86_64.egg',
'/home/cchilders/scripts/python/my_scripting_library',
...
'/home/cchilders/.ipython']
running test_stuff with from .silo_functions import * causes:
Traceback (most recent call last):
File "./test_stuff.py", line 3, in <module>
from .silo_functions import *
ValueError: Attempted relative import in non-package
running test_stuff with from my_scripting_library.silo_functions import * causes:
Traceback (most recent call last):
File "./test_stuff.py", line 3, in <module>
from my_scripting_library.silo_functions import *
ImportError: No module named my_scripting_library.silo_functions
but running test_stuff with from silo_functions import * works:
it prints the lines
Of course I can't use this package from other folders, which is the real issue- I don't want to be forced into throwing all scripts in this one place. This is causing huge problems as I am constantly reusing dozens of functions each script, and over 5 tutorials on making a folder a python package never have worked yet. Why is something on the python path with an init not a package? Thank you
May be it is because you've added '.../python/my_scripting_library' to your path. But there are no 'my_scripting_library.py' at this folder.
If you want to use 'my_scripting_library.silo_functions', try to add '/home/cchilders/scripts/python' (not '/home/cchilders/scripts/python/my_scripting_library') to path.
Because 'my_scripting_library' is module. Python will find this folder, find __init__.py in this folder and mark it as module.

Python module import error

when i am importing some modules from python shell, import work fine:
for example: (link for propy module https://code.google.com/p/protpy/downloads/list)
>>> import propy
>>>
but when i write script with python Default IDLE or with other IDE and save it to as .py script , import statements not work and generate error like this
python fragment_generator.py
>>>
Traceback (most recent call last):
File "J:\acetylome scripts\New folder\fragment_generator.py", line 1, in <module>
import propy
ImportError: No module named propy
>>>
please solve it
thanks in advance:
When you run the shell from the same directory the files are in the import will work. You are probably working from another directory when the import fails.
you can add a directory to the python path like this.
befor your import:
import sys
sys.path.append('/path/to/your/folder')
import propy
good luck.

Categories