How to get packages to import in code within anaconda virtualenv? - python

Currently I cannot import package installed in virtual environment:
Traceback (most recent call last):
File "/Users/glada/GIT_REPOS/autoflux/main.py", line 5, in
import cx_oracle
ModuleNotFoundError: No module named 'cx_oracle'

Imports in python are sometimes a bit tricky because it depends on how you call a line of code. Do you call the import from the python REPL input?
>>> import cx_oracle
Do you call the line of code from inside a script?
$ python myscript.py
Do you run the script as a module?
$ python -m myscript
Also a folder structure would be helpful. Do you use virtualenv? Maybe you have installed the package to the wrong location (installed in main python installation site-packages and now you are trying to import inside an activated virtualenv)?
There is a brilliant answer on a similar question, which is really descriptive and once I also found very useful to understand imports in python. Maybe this helps you.

Related

Using a Python Package Outside of Anaconda

I wish to run a package, openquake; however, it was installed separately from Anaconda, which I am using to run python.
[centos modules]$ which python
~/anaconda2/bin/python
I have set openquake up and I believe it's installed correctly, however, when I try to run my script, I receive the following error:
[centos maxdist]$ python ../script.py
Traceback (most recent call last):
File "script.py", line 12, in <module>
from openquake.hazardlib.source import ComplexFaultSource
ImportError: No module named openquake.hazardlib.source
I was wondering if I need to add a line to my script for it to find the openquake module for it to run successfully? In general, what's the best way to run a python package that is installed outside of the main Anaconda python instance? I am trying to avoid having to re-install openquake for Anaconda, as I was having a little bit of difficulty with that work-around.
Thank you.
If both environments, anaconda and default python that you installed the library for have the same python version, then
import sys
sys.path.append(r'/path/to/package')
from openquake.hazardlib.source import ComplexFaultSource
Check if this works. If the package is compiled for different version, then there will be an error. Also, I would suggest you to use one environment and avoid doing this for the sake of simplicity.

Modules working in Pycharm but not Shell or Command Line

I'm new to Python and having trouble with several modules. In Pycharm, everything works fine. I am able to import modules, pip installer works fine. However, for some reason I'm running into issues when trying to import modules in the Python Shell, or when running programs using the command line.
For example, I can import Beautiful soup just fine in pycharm, but when I try in the shell I get:
Traceback (most recent call last):
File "", line 1, in
import bs4
ModuleNotFoundError: No module named 'bs4'
Similarly, when I save a program in pycharm and try to run it from the command line, I get:
C:\Users\Tony>hellowness.py
Traceback (most recent call last):
File "C:\Users\Tony\MyPythonScripts\Hellowness.py", line 3, in
import selenium, bs4, pyperclip
ModuleNotFoundError: No module named 'bs4'
The correct directory is being used, because I can run simple programs that don't use modules without problems in the command line.
Sorry if this has been answered eleswhere. I searched the forum but people seem to being having trouble installing modules. The modules are installed fine (I think) they just don't work in the Python Shell or the command line. How do I go about troubleshooting this?
Thank you,
Tony
I finally figured it out. As you said there were two "Site Packages" directories installed. One for Pycharm and one for Python itself. I am sure there's a more efficient way to solve it, but for now I just copied any relevant modules from Pycharm's site packages folder over to Python's.
Thanks!

ImportError: No module named twisted.persisted.styles

From IDLE, I tried to run a script with a newly installed scrapy 1.0.3.
I'm using a script from a friend whom it worked for (but on Windows, I'm on a Mac).
From the import of scrapy on the first line, I get this error when running the program:
ImportError: No module named twisted.persisted.styles
The whole script, if it's helpful, points to this:
Traceback (most recent call last):
File "/Users/eliasfong/tutorial/tutorial/spiders/medspider.py", line 1, in <module>
import scrapy
File "/Library/Python/2.7/site-packages/scrapy/__init__.py", line 27, in <module>
from . import _monkeypatches
File "/Library/Python/2.7/site-packages/scrapy/_monkeypatches.py", line 20, in <module>
import twisted.persisted.styles # NOQA
ImportError: No module named twisted.persisted.styles
Any suggestions on how to tackle this problem?
Just try to force the update of twisted :
pip install twisted --upgrade
That works for me with python3.4 and Scrapy==1.1.0rc1
Either twisted is installed on your mac (I highly doubt it since it's not a standard library) and for whatever reason the IDE (i'm assuming that's what you mean since you typed "idle") or the terminal you are in doesn't have your updated environment variables, meaning it doesn't understand where your default python libraries are (again I highly doubt it), or you simple do not have twisted installed on your mac. If it's not installed you have a couple of options:
The easiest way to install a python package is through pip.
If that not an option you can try homebrew which is another package manager for macs. It offers an easy way to install packages correctly.
If that still is not an option for you or you simply don't want to attempt that you can download twisted directly from here (the .bz2 since you're on a mac), click on it and it should unzip it for you. Then just run setup.py and it should install it in the correct location on your mac.
If that still doesn't work and you have decent knowledge of unix. Use the "locate" command on the terminal and find out where your dist-packages directory is and put the source for twisted in there directly and then attempt to import twisted in your IDE or in the python interpreter to verify that it is installed.
note: If you're still having problems after it is installed trying restarting your IDE or messing with some setting to make sure your IDE has the right environment and python path. Hope that helps!
It could be related to having installed Python without bzip2. I had the same error and this helped me, see the accepted answer here:
Installing Twisted through pip broken on one server
Had this exact thing on FreeBSD. Solution (as root/sudo):
chmod -R go+rX /usr/local/lib/python2.7/site-packages
Some directory permissions weren't set up right on install.

Python - problem in importing new module - libgmail

I downloaded Python module libgmail from sourceforge and extracted all the files in the archive. The archive had setup.py, so I went to that directory in command prompt and did
setup.py install
I am getting the following error message
I:\libgmail-0.1.11>setup.py install
Traceback (most recent call last):
File "I:\libgmail-0.1.11\setup.py", line 7, in ?
import libgmail
File "I:\libgmail-0.1.11\libgmail.py", line 36, in ?
import mechanize as ClientCookie
ImportError: No module named mechanize
This may be trivial, but I am new to python. So plz guide what to do.
please note, I am using python 2.4 and using Windows-XP.
Thank you
MicroKernel
I think this lib depends on this one:
http://wwwsearch.sourceforge.net/mechanize/
Try installing it first.
You need to download and install the module called mechanize. Depending on your operating system (ie. Linux), your package manager probably has something for this, otherwise you will need to google it, and follow it's installation instructions.
easy_install mechanize
If this doesn't work, you need to fix your PATH environment variable to include path to your python installation directory\scripts. easy_install will save you a lot of time in future.
P.S.: Python 2.4 is 6 years old, you should really consider at least 2.6.

How can I import the sqlite3 module into Python 2.4?

The sqlite3 module is included in Python version 2.5+. However, I am stuck with version 2.4. I uploaded the sqlite3 module files, added the directory to sys.path, but I get the following error when I try to import it:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "sqlite3/__init__.py", line 23, in ?
from dbapi2 import *
File "sqlite3/dbapi2.py", line 26, in ?
from _sqlite3 import *
ImportError: No module named _sqlite3
The file '_sqlite3' is in lib-dynload, but if I include this in the sqlite3 directory, I get additional errors.
Any suggestions? I am working in a limited environment; I don't have access to GCC, among other things.
I had same problem with CentOS and python 2.4
My solution:
yum install python-sqlite2
and try following python code
try:
import sqlite3
except:
from pysqlite2 import dbapi2 as sqlite3
Did you install it? That often works better than messing with sys.path.
python setup.py install
If so, you should then find it.
If, for some reason, you can't install it, set the PYTHONPATH environment variable. Do not make a habit of messing with sys.path.
You will need to install pysqlite. Notice, however, that this absolutely does require a compiler, unless you can find binaries for it (and Python 2.4) on the net. Using the 2.5 binaries will not be possible.
You must ensure your sqlite, sqlite-devel, python-sqlite are installed correctly first and then recompile Python.

Categories