Getting Import error for module from a .pth file - python

My team is using a custom built python library. There is a .pth file containing the path to that module file which I have placed into my python site-packages directory. However, whenever I try to execute any scripts that are using the said library I get a Module not found error. I am not very experienced with importing custom modules or how the python import process works. I am using a windows 10 machine, and python 2.7

Related

ImportError: DLL load failed while importing _imaging: The specified module could not be found

I'm trying to run my python script that I developed/tested through IDE and it's working fine over there.
But when I try to run the same script on Command Prompt (Windows 10) which has to import any module. Note that python script and the .bat file is in the same directory.
I think this is happening environment setup so I did some search and found these posts below;
import error: 'No module named' *does* exist
No module error when running python script from command prompt
Python command line Import Error
Unable to imoprt modules on python script while running on cmd
Package doesn't work if run from cmd or from the .py file.... PYTHON
https://www.programmersought.com/article/7436148385/
Here is the error snapshot
Steps followed
Python version 3.8.3
Environment variables are set under system environment variables
in the 'code.py' also added
import sys
sys.path.append('D:\program_files\anaconda3\Lib\site-packages')
Seaborn and Scipy updated the latest versions
CMD FILE look like this
I followed older solutions like set up environment variables for python libraries and python path so for not even close to get rid of this error.
Any idea to help what I'm missing here ?
Thanks

ImportError: dynamic module does not define init function (initlibpyuno)

I have installed Eclipse IDE with Python, I'm trying to setup an environment for writing some python scripts to automate Libre Office, I have made a script with just one line of code for now ("import libpyuno"), when i run this line of code i get an error:
ImportError: dynamic module does not define init function (initlibpyuno)
I have added "usr/lib/libreoffice/program" as external library path.
Can anyone tell me why I'm getting this error?
The supported way of using pyuno is to invoke the python interpreter bundled with LibreOffice itself. If you want to use an existing interpreter, then you need to make sure you manually do the same setup as the python shell script does inside the LibreOffice installation set (/usr/lib/libreoffice) in your case. This involves not only setting PYTHONPATH but at least specifying URE_BOOTSTRAP as well (both are environment variables.)
Now to your actual question: you probably don't want to import libpyuno. If you want to import a single LibreOffice-specific Python module for testing, then import just uno.
See https://cgit.freedesktop.org/libreoffice/core/tree/scripting/examples/python/NamedRanges.py for a real-world pyuno script, you can see even that only needs the uno module.

Custom modules are not found when calling python script in console VS pycharm

I have a python3 script that I am calling in terminal; I do not use Python prefix to run it, since I did add #!/usr/local/bin/python3 in my script (I have python3 from brew, on OSX).
The interesting thing is that if I run the script in terminal, I get an import error because one of my custom module hasn't been found. If I run the same script in pycharm, it works fine.
I assume Python launch and read all the various path that I use for modules in the same way, in both pycharm and terminal, but it seems not the case. How do I set up my scripts so the modules are found, independently from their path?
I may run the same script from other machines too, so I want to be prepared and do the right thing from the start.
EDIT
I am running pycharm on OSX; Python3 is installed via Brew, but the symlink is in /usr/local/bin.
My script is running from a folder inside my home directory, so
/Users/tester/git/python_test_app/main/base/app_main.py
The custom modules are in the same folder of the main py script, but one level above: /Users/tester/git/python_test_app/main/pyutils.py
The import statement from app_main.py is
import main.pyutils as utilities
This is the stack trace that I get when running the script:
Traceback (most recent call last):
File "main/base/app_main.py", line 13, in <module>
import main.pyutils as utilities
ModuleNotFoundError: No module named 'main'
EDIT 2 and solution
Thanks to The answers, I was able to figure out that the issue is related to how Pycharm handle projects. Basically it somehow save the path where the project is; so calling an import will result in the project folder being parsed, and that's why it works fine from Pycharm.
In Python, unless PYTHONPATH has the path to my project or other modules that I wrote, it won't be able to find them, hence, raise the error.
FIX:
in my main module that I use to run the application, I did retrieve the path of the file; which I know being one level below the modules I need; so I can explicitly add the folder to the current sys.path. This will end up making possible for me to call the import successfully.
import sys
current_dir = os.path.dirname(__file__)
sys.path.insert(0, , current_dir)
The only downside is that every file and resource that I use in my project, has to be directly referred by full path; so I have to pass the current_dir around the various files in the project.
PyCharm has project interpreter settings. Verify these are the same as your system Python. Go to:
File menu
Settings
Project: <project name>
Project Interpreter
View the path to the Python executable/binary being used by the project in PyCharm and verify it matches what your system is calling (e.g., which python3)
Alternatively, it may be that you declared your sources root within PyCharm and the system cannot properly run the module as it exists in the path you're running it from (especially if inside a package). You can get around this using the -m parameter and calling it from Python.
You can also try running it from the Terminal inside PyCharm and see what it adds to the path before initializing the shell session (you can sometimes see this in your Run configurations also). If you are referring to modules not installed via pip / into the Python path but rather loaded into your project path, then this may be the culprit.
On PyCharm, next to the green "RUN" arrow press the box and then press edit configurations (see image)
There you'll have Working Directory - that path is where PyCharm is running that script from (without errors).
Try running it from the terminal within that path - that should solve your import errors.

Import Error for BPY module in python

I was trying to write a python script and execute it within blender context. But it reports 'Import Error : No module named "bpy"'. I tried installing another version of blender ( viz. 2.7.6 ) but as it appears there is actually no module named bpy inside "blender-2.76-rc2-win64\2.76\python\lib" , which is the path where python is searching for modules I suppose. I checked out the docs for blender but there is no explicit way of downloading bpy. Any help will be appreciated.
The bpy module is a built-in part of blender that gives python scripts access to the data and operators used in blender. This module is available to scripts run within blender itself, which includes a python interpreter. The bpy module isn't normally available to import from a normal python command, but there is limited support to compile a blender module.
To use the bpy module you can start blender and use the built in python console or you can type in or open a script in the text editor which has a run script button available. Particularly if you run scripts from the text editor you will want access to a console to get error reports.

Using custom module with Python CGI script

I'm running a python CGI script on my localhost that needs to import and use another python module that I wrote. I placed the CGI script in the Apache cgi-bin directory (I'm running this on windows). I've tried placing my custom module in the same directory, but it doesn't seem to be able to import that module. I would prefer to not have the custom module be another CGI script that is called via exec().
You need to put your Python module somewhere that Python's import can see it. The easy ways to do that are:
Make a directory for the module, and add that directory to your PYTHONPATH environment variable.
Copy the module into your Python site-packages directory, which is under your Python installation directory.
In either case, you will need to make sure your module's name is not the same as the name of some other module that might be imported by Python in your CGI script.

Categories

Resources