Problems importing a python module in Maya and it being recognized - python

Still learning certain things about Python... I am having issues recognizing my Python script in my scripts dir. First, I checked to see that my path is set correctly:
import sys
for pythonPath in sys.path:
print pythonPath
And C:/Users/..../Documents/maya/2014-x64/scripts is listed, which is where I am placing swap.py
In Maya's script editor I am typing the following:
import swap
reload(swap)
swap.printSomething()
I get:
Error: AttributeError: file line 3: 'module' object has no attribute 'printSomething' #
If I take the same code and throw it into a package...
C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py
And then call this, it works...
import swapPackage.swap as swap
reload(swap)
swap.printSomething()
Why? I am totally confused. Mel scripts even run fine from this location as well. I just can't get a simple python script to import and run.
Also something I noticed. Even though I can get this script to run in a package, the package name must be totally different than the module name. I can't have a package named this:
C:/Users/..../Documents/maya/2014-x64/scripts/swap/swap.py
but I can have one where the package name is different:
C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py

Ok folks, I was able to solve this by executing a print of my file, only to find out that it was sourcing a totally different version someone copied elsewhere. ARGH. This solves both issues, and makes sense why changing the package name from the module worked.
import swap
reload(swap)
print swap.__file__

Related

"No Module named..."-error in editor despite code appearing to work

I've been struggling for quite some time trying to import a module from a molder in a separate directory on my computer for a python project. Currently the code seems to work, but Pycharm is still giving me errors that the module cannot be found. Despite this, if I run the code it seems to do what is intended.
What I have is essentially this:
import sys
sys.path.append(r'D:\Progam\bin')
import foo
Where foo is a module found in D:\Progam\bin, and it's warning me that there is no module named foo. Considering how much issue I've for some reason had to get this working I'm hesitant to just ignore the warning if there's some underlying problem
Anyone have any idea what's happening here?
Because the file isn't in your path globally, your IDE isn't recognizing that it is then valid during execution. It would probably be a security issue if it were adding files to its path from potentially unknown code.
You could either add that directory to your path via CMD like so:
set PATH=%PATH%;C:\your\path\here\
Or just ignore the error.
EDIT: Ignore that, I'm being a sleep deprived dumbass. Take a look at:
how to manage sys.path globally in pycharm
(Thought this edit would be slightly more useful than me just deleting my answer)

Python module created with Boost.Python won't be imported

I have a big C++ module with Python 3 bindings using Boost.Python, that I compile to a .so file using CMake on macOS.
When I try to import it in the REPL, everything seems to work fine:
>>>import myModule
>>>
However, as soon as I run the import statement, the famous rocket icon of Python shows up in the Dock and stays there jumping for some minutes and stops after. Obviously then, I cannot access any of the functions defined in my module, so the import looks fine but doesn't actually do anything.
I tried looking in the Console and saw that whenever I import myModule, I get two launchservicesd[83]: SecTaskLoadEntitlements failed error=22.
It brought me to this and that related questions but I can't find what the exact problem is.
The C++ module is huge so I just can't look at the code and find the problem, thus I'm asking for any hints about at least how to debug that problem.
I can suggest the following steps:
Try to import that module though local python session. So, run interactive python interpreter, and 'import myModule'.
If bad, try to check:
are python version, with which myMoudle was linked with, is similiar to used interpreter
check that build architectires are the same
check that you can load even simple boost.python example module
If ok, check that you have correctly set up module search path in your python code.

How to call a python file, that needs to import packages?

I'm following a tutorial to call python code from a C++ program from the python docs.
Everything works just fine when trying to call the multiply example. Now if I add a line to the python source code importing a library, lets say openpyxl,
from openpyxl import load_workbook
I receive an error from python
ImportError: No module named openpyxl
I thought if I import a system library, I wouldn't have any problems, but I also get an error if I try to import datetime.
I don't have any error if I import the file from the python console. The openpyxl library is installed in my system.
So my question is: how to import python source code that needs to import packages?
EDIT: Ok, I forgot to mention something, I have not been completely honest with you guys, I'm sorry.
Trying to run the example I run into a problem: I couldn't make python found my multiply.py file, and the line PyImport_Import always return null.
My solution was to add the path in which I knew my python source was by using PySys_SetPath. The problem is that I just realized that this function doesn't append a new directory, it just overwrites the PYTHONPATH. So now python can find multiply.py, but absolutly anything else.
Of course I've deleted that line but now I have another question, why does python can't find my source if the file is just in the same directory of the C++ compiled program?
The I realized that my sys.path from my python console was a little different from the path showed in my embedded python: the first one had at the beginning of the list an empty string ''. I'm not a python expert, but when I add that line to my path I could import the multiply.py so it seems that was the reason I couldn't import modules that were located to relative to my executable was the missing of this empty path -but still don't know what it means-.
I have to thank to #paul-evans who give me the idea of adding the path to find my files.
This is what PYTHONPATH is for. You can set it as an environment variable containing a list module directories, or in the code itself something like:
import sys
sys.path.append("path/to/openpyxl/module")

IPython: ImportError: No module named mymodule

I am experience this annoying error message, every time after I have updated my module and try to reload it.
I do have a module mymodule in a package mypackage that has a __init___.py file in it.
When I do
from mypackage import mymodule
everything is ok.
After I update the module and reload it with
reload(mymodule)
Error pops up:
In [4]:
...: reload(constants)
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-264a569b44f9> in <module>()
1
----> 2 reload(mymodule)
ImportError: No module named mymodule
To resolve this, I have to kill my interpreter and re-import everything when I want to reload one module, which is extremely time-consuming and annoying. How may I fix it?
PS:
I suspect this is something wrong with PYTHONPATH, but since I am using Python tool for Visual Studio, I cannot find the PYTHONPATH option.
Update
As far as I remember, it seems that things start going wrong immediately after I have this
import os
os.chdir(constants.PROJECT_PATH + '//data//')
in one of the modules. Yet does it really matter?
I don't think it matters, as the path in the brackets is exactly my project path.
Try it:
import os, sys
my_lib_path = os.path.abspath('../../../mypackage')
sys.path.append(my_lib_path)
from mypackage import mymodule
or add your package into PYTHONPATH. For unix it:
$ export PYTHONPATH=/absolute/path/to/mypackage
Is your package in the present working directory?
When the interpreter comes across an import libraryname statement, it looks for libraryname in several locations: the present working directory, directories specified by the PYTHONPATH environment variable, and some installation dependent paths.
So as long as your module is in the present working directory, the interpreter is able to find it. However, once the pwd changes, the interpreter isn't able to find the module anymore, and the import fails. You really have two options:
Install your module in a location where Python can find it. Typically, Python packages are located in /usr/lib on Linux systems (not sure about Windows, but you could easily find out). If you put your package there, then the interpreter will pick it up. I wouldn't recommend doing this by hand; write a simple setup.py script to handle the installation for you.
Tell Python where to explicitly look for the package. This is usually done through the PYTHONPATH environment variable. Set that at the command line before invoking the interpreter (or at the system-level, if you must).
If you can't change PYTHONPATH for some reason, then you can modify the path during runtime:
import sys
sys.path.append(your_directory_here)
This is a pretty ugly way to deal with the problem, so should be a last resort.

Python 2.7: "unresolved import: ConfigParser"

I recently wrote a Python 2.7 script (using PyDev on Eclipse) that took advantage of the built-in ConfigParser module, and the script works perfectly. But when I exported it and sent it to a colleague, he could not get it to work. He keeps getting an "unresolved import: ConfigParser" error even though we are using the exact same settings. This isn't supposed to happen as ConfigParser is built-in.
I've Googled everywhere but could not seem to find any working solution. Any help would be appreciated.
ConfigParser was renamed to configparser in python 3. Chances are he's using 3 and cannot find the old py2 name.
You can use:
try:
import configparser as ConfigParser
except ImportError:
import ConfigParser
To see what's happening it may be nice comparing on both computers which sys.path is being used (i.e.: put at the start of the module being run the code below and compare the output in each case):
import sys
print '\n'.join(sorted(sys.path))
Now, if the error is not when running the code (i.e.: it runs fine and you get no exceptions), and he gets the error only in PyDev, probably the interpreter configuration in his side is not correct and one of the paths printed through the command above is not being added to the PYTHONPATH (it could be that he's on a virtual env and didn't add the paths to the original /Lib or has added some path that shouldn't be there -- or even has some ConfigParser module somewhere else which is conflicting with the one from the Python standard library).

Categories