Reinstall python and make import numpy work - python

First of all, I should say, that I checked all links at stackoverflow, but I still can't make it work. What I want is just as simple as my nose - I want to import numpy and I want to import modules created by f2py. Now, when I do in console
$ python
>> import numpy
I get an error No module named numpy. In the same way I get an error, when I try to import a fortran module made by f2py:
>> import testmodule
My OS is Ubuntu 12.04. I should also add, that I tried to uninstall and reinstall python hundreds of times with different libs and of course I did sudo apt-get install python-numpy etc. But that didn't help. What I want to hear from you, guys, is a complete step-by-step instruction (including unintallation of the current versions of python which may be corrupt and including installation instructions - download this version, unpack it to here etc.) I guess that instruction will be extremely worthy and usefull for python newbeis like me. The problem that I face now seems to be the simplest in the world, but I wonder why it has no simple solution.

Does your Python prompt have >> as the prompt? I've always seen >>> from Python.
If uninstalling Python and reinstalling doesn't work, perhaps the problem is with your user account? I'd try:
Create a new user, sudo useradd joe
Log in as the new user sudo -u joe bash -login
See if Python and numpy work now.
Exit from joe's shell (exit, logout or ^D).
Get rid of joe, sudo userdel joe
Now at least you know if the problem is with your system setup or your user setup.
Other things to look for:
run pip freeze | grep numpy or pip freeze | less to see which numpy package is (or isn't) installed.
Do you have something odd in your environment? Try env | grep -i python to see if you have a nonstandard environment variable.
Do you have python aliased to something else in your .profile or other startup? Try alias python to see if you really are starting python when you run python.
Do you have some old python in your $PATH? You can try which python and you should see /usr/bin/python. If you get '/usr/local/bin/pythonthat should be a link pointing to the "real" python at/usr/bin/python`.
Have a look at /usr/bin/python. It should be a link to python2.7.
During your uninstall-reinstall cycles, you can run pip freeze to see the list of installed packages. You should be able to make numpy appear and disappear in the freeze list when you install and uninstall it.

Related

How to use cec control in python

I am trying to write a simple loop in python that can turn of and on your tv using CEC control. But, whenever I try to "import cec" into my program it doesn't work. I am new to programing and I don't really know where to start.
looking at the error, it looks like you didn't install it. so, using your terminal in your editor, paste or type
pip install pyCEC
you can check the website here
and if it didn't work restart you editor.
then it will work perfectly.
It looks like cec is a python package: https://pypi.org/project/cec/
Depending on your python installation, you can likely install it with pip install cec.
See this documentation on getting started with python packages for more:
https://packaging.python.org/tutorials/installing-packages/

How do I completely clean-up or reset a Python installation on Ubuntu

Package management and juggling pip, anaconda, PPAs, and virtual-environments is difficult. Somewhere in my constellation of dependencies, things are broken. I'm on Ubuntu 18.04. As far as I know, I cannot fix these dependencies by hand.
The problem, for what it's worth: I've been unable to use tensorflow for a few months. Every time I try to fix it, even uninstalling and reinstalling everything to the best of my knowledge, things still don't work. After sinking enough hours, I'm looking for a "nuclear solution".
What I would like to do is cleanly remove everything except Python and any Python packages my system might require, so that I can start fresh (and hopefully do things properly.)
So, my question: How can I systematically clean up or remove my Python installation? I want to wipe everything and start anew. Does there exist a systematic guide, or a smart and reputable script that does this?
Helpful and cautionary tale, be a good idea not to delete it I think... I'll assume it was the profanity that got it deleted so I've edited it out.
!!!! WARNING !!!!
If you're like me and you think ah shur I'll do an 'apt list --installed | grep -i python' and then 'apt purge -y' all that crap, well, maybe don't.
Now my entire system is doo-doo. It all seemed fine until I rebooted and now there is no network connection, netplan and a bunch of other stuff is just gone. No recovery possible.
Actually it looks like it was an 'apt autoremove' AFTER I'd done the above that actually removed netplan.
And all because I wanted to remove multiple python versions to get over those gosh darn import issues and messin' around with pip and pip3 and pip3432432 and what gosh flippin' version of python is tied to what oody doody version of pip...
UPDATE - This video helped me recover me files https://youtu.be/tGIPeWkPkMc
You can try sudo apt purge python-pip python-dev, or python3 and python3-pip if you're using Python 3. This must remove all files/folders created by the installed packages. But I'm not really sure you need to do it.
The better solution is just to uninstall all pip packages, like pip freeze | xargs pip uninstall -y. Just to be sure you can even remove pip folder /usr/lib/python2.7/site-packages manually (or similar folder for Python 3).
(Extending #jay 's warning)
"Out of the Box", later Ubuntu versions have a system dependency on Python(3). For instance, my Ubuntu 20.04 runs "networkd-dispather" and "unattended-upgrade-shutdown". I've seen similar on various Cloud instances. Kali 2020 uses python to run a printer daemon. Check your system first. Or even try it on a VM similar to your system.
Try a quick check before doing something drastic.
ps aux | grep python
That will give you an idea if services are being run by Python. Frankly, I like the pip3 freeze to list the installed packages and cleanup from that list, like #sortas suggests in the second half of his answer.

Python: modules globally accessible [duplicate]

I have my own package in python and I am using it very often. what is the most elegant or conventional directory where i should put my package so it is going to be imported without playing with PYTHONPATH or sys.path?
What about site-packages for example?
/usr/lib/python2.7/site-packages.
Is it common in python to copy and paste the package there ?
I usually put the stuff i want to have ready to import in the user site directory:
~/.local/lib/pythonX.X/site-packages
To show the right directory for your platform, you can use python -m site --user-site
edit: it will show up in sys.path once you create it:
mkdir -p "`python -m site --user-site`"
So if your a novice like myself and your directories are not very well organized you may want to try this method.
Open your python terminal. Import a module that you know works such as numpy in my case and do the following.
Import numpy
numpy.__file__
which results in
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site- packages/numpy/__init__.py'
The result of numpy.__file__ is the location you should put the python file with your module (excluding the numpy/__init__.py) so for me that would be
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site- packages
To do this just go to your terminal and type
mv "location of your module" "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site- packages"
Now you should be able to import your module.
This is something that works for me (I have to frequently create python packages that are uploaded to a private pip repository). elaborating on the comment by #joran on the question.
create a "build directory" which is used as a workspace to create packages. any directory of your choice will do
Copy your python package dir there, and create a setup.py file. this should help in creating the setup.py correctly.
create a virtualenv for the project you are working on. virtualenvs have a bunch of other benefits, I am not going into the details here.
create a local dist package python setup.py sdist --format=tar. the package created should ideally be in the dist folder.
Install the package on your virtualenv (after activating it). pip install <yourpackage>.tar
you can use pip install --force-reinstall if you need to play around with the libraries more and re-create the dist packages.
I've found that this method works great for me. If you do not need to package the modules for use of other systems instead of just your local, this method might be an overkill
Happy hacking.
If you're developing a module I would recommend to follow this.
import sys
sys.path.append("/home/mylinux/python-packages")
Now any module you keep in python-packages is importable by Python-interpreter.
On my Mac, I did a sudo find / -name "site-packages". That gave me a few paths like /Library/Python/2.6/site-packages, /Library/Python/2.7/site-packages, and /opt/X11/lib/python2.6/site-packages.
So, I knew where to put my modules if I was using v2.7 or v2.6.
Hope it helps.
import folders could be extracted by adding following source code:
import sys
print sys.path
automatic symlink generation example would be:
ln -s \`pip show em | grep "Location"| cut -d " " -f2\` \`python -m site --user-site\`
instead of "em" you may use other package you've "just installed but the python can't see it"
below I'll explain in more details as being requested in the comment.
suppose you've installed python module em or pyserial with the following command (examples are for ubuntu):
sudo pip install pyserial
and the output is like this:
Collecting pyserial
Downloading pyserial-3.3-py2.py3-none-any.whl (189kB)
100% |████████████████████████████████| 194kB 2.3MB/s
Installing collected packages: pyserial
Successfully installed pyserial-3.3
the question would be following - python can't see the module pyserial, why?
because the location where the module has been installed isn't the one python is looking at for your particular user account.
solution - we have to create symlink from the path where pyserial arrived to the path where your python is looking for.
symlink creation command would be:
ln -s <what_to_link> <where_to_link>
instead of typing exact location we are asking pip to tell us where it stored modules by executing command:
pip show pyserial | grep "Location"| cut -d " " -f2
instead of typing exact location we are asking python to tell us where it looks for the modules being installed by executing command:
python -m site --user-site
both commands has to be escaped with "`" character (usually on the left of your 1 button for the US keyboards)
in result following command will be provided for ln and the missing symlink would be created:
ln -s /usr/local/lib/python2.7/dist-packages /home/<your_username>/.local/lib/python2.7/site-packages
or something similar, depending on your distro and python/pip defaults.

Nest simulator: python says “no module named nest”

After installing the Nest Neural Simulator, I keep getting the following error when trying to run any of the example python files that came in the installation. I've tried re-installing Nest, Python, and using Anaconda, but no go.
Python error:
ImportError: No module named nest
Suggestions?
At https://nest-simulator.org/documentation you now find many different install instructions and how to solve the "ImportError: no module named nest" depends on the way you installed NEST.
System Python
The problem with the nest python module not being found is usually, that NEST is installed for a specific Python version and you can not load it from another. So while many OS still use Python 2.7 you may need to explicitly run
$ python3
>>> import nest
Additionally, if you have multiple Python 3.x versions installed, modules may still be installed for a different version and you have to explicitly start python with python3.6 or python3.8, etc.
Conda package
As #nosratullah-mohammadi already mentioned, if you have a Conda flavour installed, using the pre-built package is a very quick solution. The link in his post is unfortunately broken; this one should work, then go to "Installation" in the side bar.
$ conda create --name nest -c conda-forge python3 nest-simulator
$ conda activate nest
$ python # this should load the Python from the conda env
>>> import nest # this loads nest which is installed explicitly for that Python
From Source
For every install from source, be sure to have Python and other prerequisites installed before building NEST. Then you can create your temporary build directory (can be deleted afterwards) and configure with the flags you need.
cd somewhere
mkdir nest-build
cd nest-build
cmake -DCMAKE_INSTALL_PREFIX:PATH=/install/path -Dwith-python=3 .../sources/of/nest-simulator
Replace somewhere, /install/path and .../sources/of/nest-simulator with the paths correct for your setup. (A popular choice when compiling from source in conjunction with Conda environments, for example, is to use -CMAKE_INSTALL_PREFIX=$CONDA_PREFIX, which installs NEST directly into the active environment. Conda is however in no way necessary for NEST.)
Add more -D... flags as you prefer. Possible flags you see with cmake -LA .../sources/of/nest-simulator, as pointed out here. You are probably interested in many of the with-xyz at the end. Check the aforementioned documentation for deatils.
Check that the paths and libraries reported in the Configuration Summary make sense (you may need to scroll up a bit to see). It could for example look something like this:
--------------------------------------------------------------------------------
NEST Configuration Summary
--------------------------------------------------------------------------------
[...]
Python bindings : Yes (Python 3.6.8: /home/yourname/miniconda3/envs/nest/bin/python3)
Includes : /home/yourname/miniconda3/envs/nest/include/python3.6m
Libraries : /home/yourname/miniconda3/envs/nest/lib/libpython3.6m.so
Cython bindings : Yes (Cython 0.27.3: /home/yourname/miniconda3/envs/nest/bin/cython)
[...]
--------------------------------------------------------------------------------
[...]
PyNEST will be installed to:
/home/yourname/miniconda3/envs/nest/lib/python3.6/site-packages
--------------------------------------------------------------------------------
In this example CMake configured everything for Python3.6 from my conda environment.
If you are satisfied with your settings and all the found Python versions match, run the usual
$ make # optionally with -j$(nproc)
$ make install
$ make installcheck
In case that works out fine you are done and can delete the build directory to free the space. Congratulations!
Also if things get too mixed up and it doesn't seem to do what you expect, it is sometimes useful to delete the build directory and start off clean.
there is a new method of installation added to other methods, wich is installing nest with conda package and it's in its beta version. but it works and it's really simple.
you can find the installation from here!
simply after install mini conda package run your terminal and type this :
conda create --name ENVNAME -c conda-forge nest-simulator python
then type :
conda activate ENVNAME
and you're good to go!
NEST now provide the solution to that problem and similar ones, by providing a script which automatically sets the relevant system variables:
If your operating system does not find the nest executable or Python
does not find the nest module, your path variables may not be set
correctly. This may also be the case if Python cannot load the nest
module due to missing or incompatible libraries. In this case, please
run
source </path/to/nest_install_dir>/bin/nest_vars.sh
to set the necessary environment variables. You may want to include
this line in your .bashrc file, so that the environment variables are
set automatically.
https://nest-simulator.readthedocs.io/en/latest/installation/linux_install.html
Turns out I needed to move the directory where I installed nest (Users/name/opt/nest) into a nest folder in the following directory in anaconda. Specifically, I moved the contents of the folder (from the nest installation):
/Users/name/opt/nest/lib/python2.7/site-packages/nest
Into this one:
/anaconda/lib/python2.7/site-packages/nest
Disclaimer: I could very well run into problems for not having copied all the contents of the Nest installation, but this little hack is helping me run example files now.

Python Pylearn2 package "ImportError: No module named pylearn2.utils"

I have recently tried to use pylearn2, a deep machin learning package for Python developed at University of Montreal.
I've just installed it and tried to run a simple example, but it did not work.
I have been using a pc with an Ubuntu 13.10 system, on which I found ipython installed.
I have installed Theano and later pylearn2, by following this webpage instructions:
http://deeplearning.net/software/pylearn2/
I have also modified the .bashrc file, as suggested
I thought that everything went well, and then I tried this Quick start example:
http://deeplearning.net/software/pylearn2/tutorial/index.html
I stopped at the first command:
python make_dataset.py
My terminal states:
Traceback (most recent call last): File "make_dataset.py", line 14,
in
Do you have any ideas on why it is not working?
Do you why these errors occur?
Thanks a lot
EDIT: the 14 line is the first non-commented line of the file. It states
from pylearn2.utils import serial
Without more information, I can only guess, but my first guess is…
You haven't actually installed pylearn2, because if you follow the linked docs to grab the git repo and add a PYLEARN2_DATA_PATH variable, nothing gets installed into site-packages (or dist-packages or anywhere else on sys.path).
This means that pylearn2 will only work when you start Python from within the top-level directory of the pylearn2 repo.
So, if you run a script like this:
$ cd /path/to/pylearn2
$ cd scripts/tutorials/grbm_smd/
$ python make_dataset.py
… it won't actually work.
It looks like there is a setup.py file in the repository. Does it work? I have no idea. Even though the docs don't mention using it, you might want to try. Either this:
$ pip install .
… or, if you don't have pip or it doesn't work on this package:
$ python setup.py install
Either way, of course, you may need sudo or a flag to install to your user site-packages instead of system, etc., as with any other Python package.
If that doesn't work, you might be able to just add /path/to/pylearn2 to your sys.path in some way. The most obvious way is by doing an export PYTHONPATH=/path/to/pylearn2:$PYTHONPATH in your ~/.bashrc.
Also, you will need to either source ~/.bashrc or create a new shell to get any effects of modifying the file.
If you're wondering why the instructions and the tutorial together don't give you enough information to make this work without a lot of hassle, I think that's covered in the very top of the documentation:
Pylearn2 is still undergoing rapid development. Don’t expect a clean road without bumps!
And the very fact that there is no PyPI download yet implies that this really is not ready for novices to use. If you don't know enough about using Python packages (and bash basics) to muddle through on your own, there's a good chance you won't be able to use this package.

Categories

Resources