Using a Python Package Outside of Anaconda - python

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.

Related

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

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.

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!

No module named 'tensorflow' error showing up in Windows 10 Python 3.6

I installed Tensorflow using the instructions on its website https://www.tensorflow.org/install/pip. I used the virtual environment option, but didn't use Anaconda.
Everything went smoothly until I tried to import tensorflow in python. I did
import tensorflow as tf
in my Python file and in return I get
Traceback (most recent call last):
File "...", line 1, in <module>
import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'
What's up with this? How can I fix it? I'm using Python 3.6 on a Windows 10 machine. All other answers to this I could find involved Anaconda which I didn't use.
Thanks in advance!
I recommend you start using Anaconda. It just makes life so much easier (especially when handling different Versions of TensorFlow)
Make sure that you are in the Environment you‘ve installed it. Note that you need to activate it first (as shown on the Installation tutorial).
Another thing you can try is installing it with pip3. Although this didn‘t always work for me (it can lead to trouble when having python 3.7 installed)
Lastly you could lookup which python is linked in the PATH and verify that the tensorflof packages are actually there.

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.

Installing Numpy with OpenBLAS support in home directory

I'm installing a Python (2.7.8) environment locally in my home folder on a server, in order to use newer versions of Python and some of its libraries (Numpy, Theano) together with OpenBLAS. For this, I first installed Python in the folder usr in my home directory (/home/my_home/usr/) as described here:
https://mail.python.org/pipermail/tutor/2002-March/012903.html
The installation seems to have gone smoothly. I checked this by starting the Python interpreter, which displays the right version on startup.
Following this, I installed OpenBLAS into /home/my_home/opt folder as described here:
Compiling numpy with OpenBLAS integration
... which also didn't give me any errors. However, now when I try installing Numpy (according to the instructions in the above link), I get the following error:
Traceback (most recent call last):
File "setup.py", line 22, in <module>
import subprocess
File "/home/ext_sxc/usr/lib/python2.7/subprocess.py", line 427, in <module>
import select
ImportError: No module named select
I suppose the straightforward thing to do is to find the package for select, compile and install it. I'm not sure if this is the right thing to do because the last time I installed Python 2.7.6 locally in a home directory, there were no such errors. As I understand, the select module should have been installed along with Python.
This also happens when I try installing IPython in a similar way in my home folder. The library which gives the ImportError is different.
I'm wondering if these are related to a more general mistake I may have made while installing Python. I'd appreciate any advice on this issue. Please let me know if any additional information would help. Thanks!

Categories