PyCharm: Difficulties importing module of package - python-2.7

I am just getting started with PyCharm and have imported a project from Eclipse.
Running on a virtual env, I was able to register a compiled pyd file (PresPy). However, I still have an "unresolved reference" error. I also installed colormath from the PyCharm tooltip recommendation.
When running the project I strangely do not have the pyd-related error. Instead, the interpreter complains about a different module and not on the 'package level'.
Error 1
Unresolved reference: 'rgb_color' in import statement:
# These 4 lines work fine
import copy
import random
from math import pi, degrees
from configuration import Color, Condition # From custom package
import colormath.color_diff
from PresPy import rgb_color # Refers to a .pyd.
Error 2
Stack trace:
....
File "x:\proj\src\color_logic\palette.py", line 14, in <module>
import colormath.color_diff
ImportError: No module named colormath.color_diff
Process finished with exit code 1
Content root is the project folder. Source folders are in a separate /src/ folder. An incomplete setup.py stub is located in the content root. The project ran without errors from Eclipse on a different installation of pip install:ed packages.
Below, this returns the source root folder (from related answer):
import os
print os.getcwd()
Under Project: ... > Project Interpreter1 the packages colormath and PresPy are both listed.
1: PyCharm Community Edition 2016.2.3
Related: 1, 2

Solution: The Run configuration has to be changed to the corresponding virtual environment.

Related

MySQLdb import works from command line but crashes in PyCharm

I am using a Python 2.7 virtualenv with the MySQLdb package installed.
If I run Python from the command line and execute import MySQLdb, this works without error. If I run it from the PyCharm terminal, however, I get an error:
ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or directory
The same pattern occurs if I execute a file test.py containing the line import MySQLdb. It works when executed from the command line and crashed when executed from PyCharm.
I have googled the error and it seems that uninstalling and reinstalling MySQLdb could fix it. But I would like to understand why the error only occurs in PyCharm.
I have made sure that both the command line and the PyCharm terminal use
the same virtual environment (by checking sys.executable)
the same working directory (by checking os.getcwd())
the same path (by checking sys.path)
I have also checked that PYTHONPATH is undefined.
What other difference could there be?
You have to point pycharm to your virtualenv. Go to settings -> project interpreter and give pycharm the path to your python executable. Once there it should work. Note if you have a hybrid WSL/windows setup you will need one virtualenv for WSL and a separate virtualenv for windows/pycharm.

PyInstaller runs fine but exe file errors: No module named, Failed to Execute Script

I am running the following code:
pyinstaller --onefile main.py
main.py looks like:
import sys
import os
sys.path.append(r'C:\Model\Utilities')
from import_pythonpkg import *
......
import_pythonpkg.py looks like:
from astroML.density_estimation import EmpiricalDistribution
import calendar
import collections
from collections import Counter, OrderedDict, defaultdict
import csv
....
By running the pyinstaller on main.py, main.exe file is created successfully.
But when I run main.exe it gives error with astroML. If I move astroML to main.py from import_pythonpkg.py, there is no error with astroML. Now I get error with csv.
i.e. if I change my main.py to look as:
import sys
from astroML.density_estimation import EmpiricalDistribution
import os
sys.path.append(r'C:\Model\Utilities')
from import_pythonpkg import *
......
The astroML error is no longer present when I run main.exe.
There is no error with import calendar line in import_pythonpkg.py at all.
I am not sure how to handle this random error with packages when running main.exe after pyinstaller run.
import_pythonpkg is located at r'C:\Model\Utilities'
Edit:
Error with main.exe looks as following even though the original main.py runs fine. Pyinstaller was even able to let me create the main.exe without error.
Traceback (most recent call last):
File "main.py", line 8, in <module>
File "C:\Model\Utilities\import_pythonpkg.py", line 1, in <module>
from astroML.density_estimation import EmpiricalDistribution
ImportError: No module named astroML.density_estimation
[29180] Failed to execute script main
I believe PyInstaller is not seeing import_pythonpkg. In my experience, when adding to the path or dealing with external modules and dlls, PyInstaller will not go searching for that, you have to explicitly tell it to do so. It will compile down to an .exe properly because it just ignores it, but then won't run. Check to see if there are any warnings about missing packages or modules when you run your PyInstaller command.
But how to fix it...If indeed this is the issue (which I am not sure that it is) you can try 3 things:
1) move that package into your working directory and avoid using sys.path.append. Then compile with PyInstaller to so see if this works, then you know the issue is that pyinstaller is failing to find import_pythonpkg. You can stop there if this works.
2) explicitly tell PyInstaller to look there. You can use the hidden-import tag when compiling with PyInstaller to let it know (give it the full pathname).
--hidden-import=modulename
for more info, check here: How to properly create a pyinstaller hook, or maybe hidden import?
3) If you use the spec file that PyInstaller creates, you can try adding a variable call pathex to tell PyInstaller to search there for things:
block_cipher = None
a = Analysis(['minimal.py'],
pathex=['C:\\Program Files (x86)\\Windows Kits\\10\\example_directory'],
binaries=None,
datas=None,
hiddenimports=['path_to_import', 'path_to_second_import'],
hookspath=None,
runtime_hooks=None,
excludes=None,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,... )
coll = COLLECT(...)
for more information on spec files: https://pyinstaller.readthedocs.io/en/stable/spec-files.html
(notice you can also add hiddenimports here)
This answer may also prove helpful: PyInstaller - no module named
It is about to module which loaeded on your computer. If your IDE is different from your environment, you have to load same modules on your device via pip. Check the modules on CMD screen and complete the missing modules.
Sometimes you must load the modules all IDEs on your device. In my case, there were two IDEs (pycharm and anaconda). I used pycharm but pyinstaller used anaconda's modules so i unistalled anaconda and tried again. now it works..

py2exe fails to resolve ibm_db dependency (DLL)

I am trying to create an EXE using py2exe for a desktop app I wrote with python 2.7.
The app utilizes ibm_db (v2.0.7), which installs to site-packages as an .egg folder.
The contents of the ibm_db.py file are very short, as the logic itself is implemented in the DLL:
import os
if 'clidriver' not in os.environ['PATH']:
os.environ['PATH'] = os.environ['PATH'] + ";" + os.path.join(os.path.abspath(os.path.dirname(__file__)), 'clidriver', 'bin')
def __bootstrap__():
global __bootstrap__, __loader__, __file__
import sys, pkg_resources, imp
__file__ = pkg_resources.resource_filename(__name__,'ibm_db_dlls\ibm_db.dll')
__loader__ = None; del __bootstrap__, __loader__
imp.load_dynamic(__name__,__file__)
__bootstrap__()
When I ran py2exe on the project, the dependency on ibm_db wasn’t properly handled (the DLL wasn’t taken into the dist), so running the exe file stumbles upon the following error:
...
File "ibm_db.pyc", line 10, in <module>
File "ibm_db.pyc", line 6, in __bootstrap__
ImportError: No module named pkg_resources
I tried to copy the DLL manually into the dist folder (both directly and under ibm_db_dlls subfolder), but it’s of no use.
I found this page, and realized that py2exe has an issue with eggs:
http://www.py2exe.org/index.cgi/ExeWithEggs
As far as I understand, unzipping the egg is not the way to go in my case, because my program does use pkg_resources (through ibm_db.py). Still, I tried installing ibm_db via easy_setup with the --always-unzip option, but the installation gave me this error:
Not a URL, existing file, or requirement spec: '--always-unzip'
and it was still installed as an .egg.
So I’m looking at the “Including .egg files in your dist directory” option, and wondering whether it can help. And if so – where to start?
I’m using this simple setup.py for running py2exe:
from distutils.core import setup
import py2exe
setup(console=['MyApp.py'])
What is this “TurboGears” mentioned there? It doesn’t look like something I’m using in my app.
Any advice will be appreciated.
Thanks.

Cython module imports suddenly result in undefined symbol errors on Ubuntu 16.04 when previously working

Cython no longer works appropriately on my version of Ubuntu, this appears to be related to me installing either Clion or Pycharm (both professional editions), as building worked fine previously. Anaconda version = 3.5.2 using python 3.5.2, The code is simple:
# setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
source = ["cythonsrc/testing.pyx"]
extensions = [Extension("testing", source, language='c++',
extra_compile_args=["-std=c++11", "-g"],
extra_link_args=["-std=c++11"])]
setup(
ext_modules = cythonize(extensions)
)
pyx file
#testing.pyx
#!python
#cython: language_level=3, boundscheck=false
# distutils: language=c++
def test2():
print("HEF")
Build results
python cythonsrc/setup.py build_ext --inplace
/home/name/anaconda3/lib/python3.5/site-packages/Cython/Distutils/old_build_ext.py:30: UserWarning: Cython.Distutils.old_build_ext does not properly handle dependencies and is deprecated.
"Cython.Distutils.old_build_ext does not properly handle dependencies "
running build_ext
Module import results
>>> import testing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /home/name/Documents/CythonTest/testing.cpython-35m-x86_64-linux-gnu.so: undefined symbol: _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERmm
Not sure what would cause this issue, but google is failing me, I've seen people talk about g++ versions causing issue with this, however this doesn't appear to be my problem as no one has had this issue with the g++ version currently installed, and it wouldn't matter anyway I need the build process to be cross platform (and thus can't enforce g++ versions). Other people with similar undefined symbol errors are due to setup.py user error, however I've used this process with other projects on other systems and it worked, and now previous projects that were building fine no longer build on my system with out editing the code.
My inclination is that I must have downloaded or updated something (such as the Jetbrains products) that have caused some issue with the normal build or environment variables on my system.
EDIT:
I should mention the actual C++ build process works fine for .cpp .h files outside the context of Cython, I've read people mentioning python 2.7 has issues that might cause these issues but couldn't see any solutions out of that (in addition to me not using 2.7).

redirecting python import to another module

I'm working on a large open source Python project, which has modules used by both the project and other projects. The goal is to move some of these modules out to a new "library" project that can then be imported by the original project and other projects.
To make this transition smooth, the thought was to copy the modules over to the new project, and have the original project then use the new import. However, to allow other project to have time to migrate later, the thought was to have the original module redirect the import.
For example, the usage is like this in repo 'neutron' (other projects could do the same):
cat neutron/consumer.py
from neutron.redirected import X
print(X)
The in the new 'neutron_lib' project created, the module looks like this (the same as what the original was in project 'neutron'):
cat ../neutron-lib/neutron_lib/redirected.py
X = 5
In the 'neutron' project, I'm trying to do this as the redirect module:
cat neutron/redirected.py
import neutron_lib.redirected
import sys
sys.modules['neutron.redirected'] = neutron_lib.redirected
When I run pylint, it gives these errors:
************* Module neutron.redirected
E: 1, 0: No name 'redirected' in module 'neutron_lib' (no-name-in-module)
************* Module neutron.consumer
E: 1, 0: No name 'X' in module 'neutron.redirected' (no-name-in-module)
If I run this, it runs fine, and consumer.py prints '5'. If I use ipython and load consumer.py, I can see 'X' in dir() output.
Any idea why I'm getting this pylint error? Is it a false error? Is there a way to override it?
Looks like, when running under tox, I can add the following to .pylintrc to hide the errors/warnings
no-name-in-module
nonstandard-exception
When I run pylint it passes now, as does running the Unit tests. Just wish I understood why I'm getting these errors/warnings though.