I have something like the following structure in my python package:
package/
--functions/
--functionsA.py
--functionsB.py
--utility/
--utils_printing.py
--utils_server.py
My goal is to be able to import package within any of the contained .py files in a way which works when I execute a specific function in one of the .py files from the command line.
Right now, if I want to use a server function in functionsA.py then I just throw a
from package.utility.utils_server import server_function
at the top of functionsA.py. Debugging with PyCharm, this works.
Now I'm trying to use pyheat to show me where my code needs to be optimized, but it requires that I run a given file as a script from the command line, but if I try to run pyheat functionsA.py then I'll get an error like ModuleNotFoundError: No module named 'package'.
Related
I've written a custom skill for azure using regex in python, the goal is now to import the function from the file its written in, to the skill build file so it can be referenced in the function.
I keep getting the ModuleNotFound error, although when I start typing it in, VS code is recognising it and offering it as an autocomplete. I think i'm just messing up somehow in my file structuring; although i've tried a few different options and none of them seem to work.
Currently the structure is:
Categoriser Function - the overall folder
. pycache.py
.__init__.py
.azurefunction - the file i'm trying to import into
.assorted other files for the project
.Categoriser - the subfolder that the module is in
.__init__.py
.categoriser.py - the file I want to import to the first file
I've tried shifting the Categoriser folder around so it's not a subfolder of CountInstance, i've tried taking Categoriser out of the folder and popping it just into the main directory, and i've tried doing the file path route, for example:
import Categoriser.Categoriser
Briefly I thought i'd got it working by trying to import to a different .py file in the folder, but that only seems to be if I use the button de-bug and run option in VS code, if I use the keyboard shortcut it gives me the ModuleNotFound error again.
I'm guessing there's something really obvious i'm missing? I just can't seem to find it in the official documents or in a whole load of googling!
I have written a python library (ak_sql.py) to query my sql server and written another ".py" script (query_sql.py) to invoke this library and return data in dataframes.
I want to "import query_sql" in "Python srcipt" in PowerBI, so i can load those dataframes.
I invoked PowerBi from powershell from the root of "query_sql", like below:
C:\Users\akshat\Documents\ak_sql> C:\Users\akshat\AppData\Local\Microsoft\WindowsApps\PBIDesktopStore.exe
However, PowerBI was unable to find the module.
I tried printing "os.getcwd()", this is the output:
C:\Users\akshat\PythonSriptWrapper_{hashkey}
Any inputs on how this can be done?
Try to include the empty file __init__.py in the ak_sql directory and make change in your script on
from ak_sql import query_sql
So seems like it was a PYTHONPATH issue. Was a able to take care of it with appending sys.path:
import sys
sys.path.append(r"C:\Users\akshat\Documents\ak_sql>")
I am trying to run a python script and I wanted d to run it with a more pythonic way rather than maybe calling subprocess.run().
I found the runpy library and I am trying to call my script like this:
runpy.run_path(path_name='/home/dir1/dr2/dr3/run_code.py', run_name='__main__')
This returns me and error that one of the modules used in the run_code.py does not exist:
ModuleNotFoundError: No module named 'fluent_logger'
When I run the same script with subprocess.run() I get no errors.
Am I using this the wrong way?
I have two .py files I wrote that I've imported into a third .py file to use:
(The top of driver.py)
import wafer_diagram
import LightIV
wafer_diagram.py and LightIV.py contain functions that I'm using in driver.py. However, whenever I have a cleared/restarted kernel, I have to run each individual .py file, otherwise they aren't defined in driver.py. Is there a way to automatically do this when I run driver.py? I am using Spyder with python 3.8. Thanks!
Edit: To clarify, after running wafer diagram.py and LightIV.py, I am able to use the functions in driver.py without issue. However, if I restart my kernel, and then try running driver.py, an error will throw that the two modules do not exist.
I was being very silly! Although I imported my other files, I was not calling on their functions correctly. For example, for the function print_struct() from LightIV.py, I would write in driver.py:
import LightIV
print_struct()
Instead, I should have written:
import LightIV
LightIV.print_struct()
The reason that I was able to get away with this for so long was likely due to how Spyder saves variables. I would run LightIV.py and wafer_diagram.py, "saving" their functions, and then using them later on instead of properly importing them.
I've got a strange issue with importing a module. I've got a virtualenv setup and one module is available in {env}/lib/python2.6/site-packages/pkgname. There is a __init__.py file and pkgname.py inside.
Now, if I run {env}/bin/python and execute import pkgname.pkgname, it works just fine. But if I create a script in {env}/bin/pkgname.py with contents:
#!{env}/bin/python
import pkgname.pkgname
if __name__ == "__main__":
pkgname.pkgname.run()
this fails trying to import the same file again (since the package and the file have the same name). How can I disable looking in the same directory? Or how can I force the import to first look at the global packages?
Alternatively, what's the "proper" way of doing this? Just for consistency, I'd rather call my startup script the same as the actual package it's trying to run.
Call it pkgname. Done. OK, then it won't start if you doubleclick in it WIndows, but that's usually not a problem.
You can modify sys.path. It's just a list of paths to search and the current folder should be the first entry. Your file should run if you move the current folder to the end of the list. But I general I would not do something like that without a VERY good reason. Isn't it possible to rename your file name.py, runpkgname.py or something like that?