Using MacPorts to install modules via a certain path - python

I realize this is a very newbie question, but previous threads haven't been encouraging (see details below).
I've been using anaconda for most of my work via the IPython Notebook, python 2.7
However, there is a module which I could only properly download with MacPorts. Hence the current problem: when I try to use the Python command line in Terminal or use IPython notebooks, I will enter
import py-module
and I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named py-module
So, obviously the $PATH is wrong. All my previous modules have been saved with conda to /anaconda/bin, but using sudo port install py-module is going to save in a different file, /opt/local.
Is it possible to set the path such that MacPorts will install a module into anaconda/bin
such that I can import this module on ipython notebook via conda?
Previous threads have suggested simply choosing one or the other, i.e. removing anaconda entirely
rm -r ~/anaconda
and then just reinstalling all packages via MacPort. But I would prefer not to do this. Can't I re-direct the path such that
sudo port install py-module
installs the module into `/anaconda/bin/' ?
For previous takes, see
Getting PyGame to import in Python interpreter after MacPorts install

While you could adjust your PYTHONPATH environment variable or modify the path using sys.path.insert, you should not try to mix a Python module built for MacPorts' Python with Anaconda. This may work just fine if the module is python-only, but as soon as the module contains compiled code it is not certain that the two installations are binary-compatible. In the best case, loading the module will fail if it isn't compatible, in the worst case, it will randomly crash.
So, yes, you should decide for one Python and just use that.

Related

Trying to import python modules returns ModuleNotFound Error

Im attempting to use the numpy module in webots but whatever I try I always get
INFO: assignment_2_controller_2: Starting controller: python3.6 -u "assignment_2_controller_2.py"
[assignment_2_controller_2] Traceback (most recent call last):
[assignment_2_controller_2] File "assignment_2_controller_2.py", line 4, in <module>
[assignment_2_controller_2] import numpy
[assignment_2_controller_2] ModuleNotFoundError: No module named 'numpy'
WARNING: 'assignment_2_controller_2' controller exited with status: 1.
I have installed numpy with both pip2 and pip3 and neither seem to have made any difference and the package shows up in ~/.local/lib/python3.6/site-packages. I can also confirm that this module works in the python editor and in other python files.
Is there perhaps something I need to change in webots python Makefile or perhaps in my environment variables?
Edit:
Printing sys.path shows
'/snap/webots/14/usr/share/webots/lib/controller/python36', '/snap/webots/14/usr/lib/python36.zip', '/snap/webots/14/usr/lib/python3.6', '/snap/webots/14/usr/lib/python3.6/lib-dynload', '/snap/webots/14/usr/lib/python3/dist-packages'
I installed webots using snap but why wouldnt it use the python dictated by the PATH environment variable?
Fix:
By the looks of it snap creates a contained filesystem for webots to run in by design in which it installed its own python binaries. I therefore uninstalled webots with snap and then installed from the .deb file off their website and now it works fine.
Webots installed from the Snap package is running in a kind of sandbox, it is therefore using the version of Python distributed with the snap package (which you can't alter).
As explained in the documentation, you might use external controllers to launch yourself the controller instead of letting Webots starting it. This way you can chose which version of Python you want to use: https://cyberbotics.com/doc/guide/installation-procedure#installing-the-snap-package
Alternatively, you can use a different way to install Webots (from the Debian or archive package). The other installations are not coming with Python and are using the system Python.

So i installed numpy . But when i call it in a program an error occurs. Any method to solve it permanently in windows 10

Here is the error
import numpy
Exception has occurred: ModuleNotFoundError
No module named 'numpy'
File "C:\path\to\file\32.py", line 1, in <module>
import numpy
Let me know how did you install the NumPy package; using pip or something else?
If you have multiple python versions, i.e. 2.x and 3.x at the same time, please make sure your interpreter for the 32.py file is the version that you installed NumPy on.
To possibly fix your problem, you should first try installing it and see if there are any errors. You should also check the version of Python you are running on Windows 10, because when you update Python it sometimes switches names between py and python
As you can see, the version of Python has changed between py and python so you should try changing that first.
If this does not work, you should try finding the directory for NumPy and adding it to the system PATH in your script. The installer usually shows you the location by doing the following:
import sys
sys.path.append("<insert numpy location here>")
import NumPy
This should manually force it into finding the package. If none of this works, please tell us and we should be able to find a different solution.
Happy Coding!
If you're using a code editor like PyCharm, you could install it by clicking on
file then settings then the project interpreter setting and install new module! You can search for the module and install.
Make sure that the python version that you want to use is a Windows Environmental Variable. You can test this by running this line in your command line.
> python --version
If you get some other python version that is not the one that you wish to use you can set the python version you want by finding where exactly your Python folder is located and go into settings and set the path as a new variable (I can leave a tutorial for you). If that is too much of a hassle, the Python installers can set the python that you will install as an environmental variable for you. (You could uninstall and reinstall and make sure that you allow it to make it an environmental variable.
After that you should be able to import whatever external packages you want using pip
for example:
pip install numpy

Importing NumPy to Geany (Python 3.5.1)

There is a bunch of pre-made code I simply have to run. However, the code requires the module NumPy, which for some reason is not installed. I am using Geany for the Python code, and when I click compile, the console states:
"Traceback (most recent call last):
File "chi2Fit.py", line 1, in <module>
import numpy as np
ImportError: No module named 'numpy'
I really tried to look it up, and I found the zipped NumPy packages on SourceForge, but I cannot install them. I am using Windows 10 (Virtual Machine is buggy, unfortunately). I usually run my Python programs by compiling them directly and don't use the commands within the console (so honestly I don't know how to open the command line. Yes, I am a beginner). What is the explanation?
Geany is just an editor, with the ability to run commands and such from menus and buttons. So you should be able to run this code 'by compiling them directly'. If that works while the 'geany compile' does not, then you need to check the 'compile' command. Is it using the right Python?
'compile' really doesn't make sense with Python code. You are just executing a script.
NumPy is a large package that does include compiled components. So installing it requires more than downloading a ZIP file. On Windows it is usually best to install one of the precompiled packages. Anaconda is popular one.
But we know nothing about this pre-made code, and whether it has included the necessary modules or not. You need to find out from the source of that code what is needed to run it.
You need to figure out how to install NumPy. It depends on what OS you are using.
There are pre-compiled packages for Windows. There is plenty of information here on SO (e.g., Installing NumPy on Windows) or you can use Google.

ImportError: No module named gdal

My GDAL is showing strange behaviour when I try to use ".py" functions from the command line:
1. For example, when I run gdalinfo --version, I get the standard response:
GDAL 1.11.3, released 2015/09/16
2. Also when I run gdalwarp, I get the standard response showing me the flags and everything.
3. Problem:
However, when I run gdal functions with a .py extension, for example gdal_polygonize.py, I get the following:
Traceback <most recent call last>:
File "C:\OSGeoW64\bin\gdal_polygonize.py", line 36, in <module>
import gdal, ogr, osr
ImportError: No module named gdal
I checked my Path variable, reinstalled python and gdal, but nothing worked.
When I run the gdal_polygonize.py function from my QGIS it works. I just can`t use it from my command line.
There are two parts to GDAL: the GDAL utilities (gdalinfo, gdalwarp, etc.) and the GDAL Python Bindings (when you call from osgeo import gdal from within a Python script.). The two can be (or could be in the past) installed separately.
The fact that you "see" gdalwarp on the command line means that you have the location of the utilities on your PATH (environment variables). Some of the utilities are self-contained, some require the Python bindings (notably those that require calling a .py file). In order for the Python bindings to work, GDAL has to be on the PYTHONPATH environment variable.
Find the GDAL folder (will contain gdalwarp, for instance). Find the osgeo folder in Python's Lib/site-packages. Add both of these locations to PATH and PYTHONPATH. You should now be able to from osgeo import gdal in a fresh Python shell.
If you cannot, either you are missing some files in those locations (bad install), or you have not specified the path correctly / set the right environment variables. There's no other magic involved, it's either installed and linked correctly, or you did something wrong.
Note that for environment variables to be set and working, you need to "Apply/OK" the environement variables window(s), and also start a fresh command prompt / Python shell.
make sure you install the conda version of gdal. I never really got gdal to work properly until I installed it from conda. Make sure you have the anaconda python distribution. https://www.continuum.io/downloads

Conflicting versions of python in ubuntu

So i had python 2.7.2 on my server and i needed to update it to python 2.7.3. So i've tried to remove the 2.7.2 version and then install the new one using the sources. I wasn't able to remove the 2.7.2 version cause the system uses it to run crucial services on server, so i installed the 2.7.3 version in hope that after that i would be able to remove the old version. Still i cant remove the old version, although i'm able to execute the python 2.7.3 when i install any module i cant import it. I added the path to sys.path and i started finding the module but importing it causes another errors.
My python executes the /usr/local/bin/python which is the 2.7.3 version where the problems are.
If i try to execute python like this /usr/bin/python it executes the old version and everything works fine there, i can import the new installed modules.
So what can i do to make python 2.7.3 work?
I've searched a lot of tutorials and tried things like add the library in .pth files on python and i started finding the modules but when importing it i get errors like this:
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/usr/local/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/usr/local/lib/python2.7/dist-packages/numpy/core/__init__.py", line 5, in <module>
import multiarray
ImportError: /usr/local/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS4_AsUnicodeEscapeString
Thanks for the help
EDIT PROBLEM SOLvED
So to solve the missing import modules i created a .pth file under /usr/local/lib/python2.7/site-packages/ with the directories where the python modules are and the python starts to find them.
To fix the comptability problems you can install python from sources and specify the unicode doing ./configure --enable-unicode
more information here
Do not EVER mess with system python, EVER.
What you should do is install python 2.7.3 with a --prefix into your home directory, then use virtualenv -p /home/myuser/path/to/python.
In any case, using virtualenv to run your own application is almost always a good idea, as it avoids polluting the system package directories with libraries you use in your own applications.
It looks like the modules you've installed were built against your old version of Python, or at least a version incompatible with your newer installation. The import error you're seeing at the bottom is the numpy module searching for a symbol that is not in your build of 2.7.3. There is further information here.
If possible, it's usually way easier to upgrade Python with a package manager. That way, if anything on your system depends on Python, but does not need exactly 2.7.2, then Python can be easily upgraded without disturbing anything. I'm guessing that either your server doesn't have a newer version of Python available and you can't add new repositories, or you don't have access to a package manager. If using packages is possible, I would go ahead and remove what you've built from source (the command should be 'make clean' if Python uses GNU Make).
If that isn't an option, then there should be a way to compile Python, but not install it into system directories. Then you could add a symlink for users, and make sure that symlink has precedence in their path.
When installing python use the following steps
using prefix to specify the installation directory
./configure --prefix=/usr/bin/python
make
make install
Then everytime u run a new Terminal u have specify
export PATH="$PATH:/usr/bin/"
to tell where is the installation directory of Python
This way u can use any number of pythons
You can install python libs from R. It works for me.
For example, to install numpy library from R type:
system('python -m pip install -U numpy')

Categories

Resources