Python can't locate modules which I have created - python-2.7

I am new to programming and have recently installed the Enthought Canopy distribution and can't seem to import certain modules.
Python 2.7 MacOSX
Numpy works when I import it, however other modules which I have created or downloaded as a simple module.py file return this error message:
import numfun1
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-297-cb46e477a372> in <module>()
----> 1 import numfun1
ImportError: No module named numfun1
Could it have something to do with where those modules are saved? If so, how do I point python in their direction? or where should I put those modules so that Python sees them.
Thank you in advanced for your suggestions.

Information about the module search path is included in the official Python tutorial: http://docs.python.org/2/tutorial/modules.html#the-module-search-path.
A lot of python libraries come with a setup.py script that will automatically install them into locations that are on the search path.
The installation process can be even more automated by using a Python package manager like pip.

If you create a module you must put it where your script is.

Related

Cannot import sklearn_pandas in Google AI Platform Notebook after pip install

I am playing around with Google's AI Platform Notebook (which is still in beta at the moment). I uploaded a python script that has dependency on sklearn_pandas and I am getting this error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-24f79569b871> in <module>
----> 1 from sklearn_pandas import DataFrameMapper
ImportError: No module named 'sklearn_pandas'
I then try to pip install it:
!pip install sklearn-pandas
I restarted the kernel but it is still getting same ImportError. I did this to confirm sklearn pandas is really installed:
!pip list |grep -i sklearn
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
sklearn-pandas (1.8.0)
Anyone has tried this? I used google colab before this, and I never have such difficulty. It will be great if Google colab level of user experience can be ported into this beta product. I think this could be a general python module importing issue than just sklearn_pandas in particular.
i think it may be
%pip install sklearn-pandas
instead of "!pip".

Odoo custom module with external Python library

I created an Odoo Module in Python using the Python library ujson.
I installed this library on my development server manually with pip install ujson.
Now I want to install the Module on my live server. Can I somehow tell the Odoo Module to install the ujson library when it is installed? So I just have to add the Module to my addons path and install it via the Odoo Web Interface?
Another reason to have this automated would be if I like to share my custom module, so others don't have to install the library manually on their server.
Any suggestions how to configure my Module that way? Or should I just include the library's directory in my module?
You should try-except the import to handle problems on odoo server start:
try:
from external_dependency import ClassA
except ImportError:
pass
And for other users of your module, extend the external_dependencies in your module manifest (v9 and less: __openerp__.py; v10+: __manifest__.py), which will prompt a warning on installation:
"external_dependencies": {
'python': ['external_dependency']
},
Big thanks goes to Ivan and his Blog
Thank you for your help, #Walid Mashal and #CZoellner, you both pointed me to the right direction.
I solved this task now with the following code added to the __init__.py of my module:
import pip
try:
import ujson
except ImportError:
print('\n There was no such module named -ujson- installed')
print('xxxxxxxxxxxxxxxx installing ujson xxxxxxxxxxxxxx')
pip.main(['install', 'ujson'])
In python file using the following command, you can install it (it works for odoo only). Eg: Here I am going to install xlsxwriter
try:
import xlsxwriter
except:
os.system("pip install xlsxwriter")
import xlsxwriter
The following is the code that is used in odoo base module report in base addons inside report.py (odoo_root_folder/addons/report/models/report.py) to install wkhtmltopdf.
from openerp.tools.misc import find_in_path
import subprocess
def _get_wkhtmltopdf_bin():
return find_in_path('wkhtmltopdf')
try:
process = subprocess.Popen([_get_wkhtmltopdf_bin(), '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except (OSError, IOError):
_logger.info('You need Wkhtmltopdf to print a pdf version of the reports.')
basically you need to find some python code that will run the library and install it and include that code in one of you .py files, and that should do it.

python shell import pandas

I have been using pandas and numpy in the iPython Notebook for Python 2.7 and 3.5 without problems for some time.
Now I am switching to the Python Shell for some tasks, but when I type import pandas in the shell, I receive the following error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pandas
ImportError: No module named 'pandas'
What is going wrong?
For some reason your python shell is using different environment variables that your ipython notebook. Could be due to various reasons, maybe you've installed python from the official website, and another distribution such as anaconda (happenned to me for this reason).
To check if that interpretation is correct, look ar the result of the following in both environment (shell & notebook)
import sys
print(sys.path)
Most likely you will find different folders. Python can only import for subfolders of those folders. I guess some will be missing in your shell.
You have 3 approaches :
The easiest way out is to also use ipython for your default shell. It should have the same environment than the notebook.
If you really need python shell to use pandas (but there shouldnt a need), you'll have to add the missing python related folders to the PYTHONPATH environment variable (how to do exactly depends on your OS, you should find easily by googling around). But it may be a pain.
Alternatively, you can start using virtual environments. You can use either conda or virtualenv. They allow you to create have different environements using different python libraries. It's a very useful skill to have with python. I suggest you to check this.
If you're doing data analytics / data science, use conda. Else use virtualenv.
starting with conda
virtualenv

Python import error when using PBS

My institute has some parallel computers with CentOS release 6.4 and Python2.6 installed. I want to use python 2.7, then I installed python 2.7.11 locally (without root permission, because I am not a root). My problem is that when I use Portable Batch System (PBS) to run python scripts, some import statements raise errors while packages were definitely installed (locally). For more detail, my scripts are:
test.py:
import ssl
test.pbs:
#!/bin/csh
cd working_dir
python test.py
When I run python test.py, no errors displayed.
When I submit the PBS job qsub test.pbs, the job was started and ended with error:
Traceback (most recent call last):
File "test.py", line 1, in
import ssl
File "/home/s1310007/python/lib/python2.7/ssl.py", line 97, in
import _ssl # if we can't import it, let the error propagate
ImportError: /home/s1310007/python/lib/python2.7/lib-dynload/_ssl.so: symbol SSL_get0_next_proto_negotiated, version libssl.so.10 not defined in file libssl.so.10 with link time reference
I guess when I ran python test.py on the login node (I logged in using ssh), the environment paths were set correctly. When the PBS job was started, the different environment paths were set, then the dynamic symbol SSL_get0_next_proto_negotiated was not resolved. I tried to modify my .cshrc or .dtprofile, .modulesbeginenv; but nothing works. Any suggestions? Thanks!

Import error web "no module named web"

I did an web application using web.py and after a while I realize that my python compiler was configure to use python2 instead of python3 (both are installed). With python2 my application was working but I got string in unicode so I try to execute my code using python3:
python3 mycode.py
But I get different kind of error, the lib urlparse was not found (this problem was cause by a new name in python3 which is urllib) but I got a problem with:
import web
The return error is:
ImportError: No module named 'web'
I can't figure out where the problem come from.
Web.py cannot work with python3, this is the linked to the gitub project.
https://github.com/webpy/webpy/issues/108
Maybe you have installed web in your python2 lib but you have none in python3 lib... Check it out