How python organized its code in the folder? - python

I am beginner for python and get stack at how python organize its code.
For example:
https://github.com/HIPS/neural-fingerprint/blob/master/examples/regression.py
In the regession.py, it will import the neuralfingerprint directory. When I run the regession.py in anaconda python, it says that the neuralfingerprint doesn't exist. I need to copy neuralfingerprint again to make sure neuralfingerprint and regession.py are in the same folder.
Any convient way? or why the author put regession.py and neuralfingerprint put them in different folder?
Thanks.

That is because what you have linked is a python library, and should be installed before you can properly use it. From the command line, run python setup.py from within the folder, or just run pip install git+https://github.com/HIPS/neural-fingerprint.git on the command line. This will install the library, and python will be able to find the correct files.
For this library, however, some other libraries are needed that are not installed automatically.
To install scipy: pip install scipy or conda install scipy with anaconda
For RDKit: it seems like you have to follow this

Related

Python 3.6: Import subdirectorys not working in Pycharm

I have a Pycharm project where I have copied two github projects (download zip and copy paste into project). My problem is that I cannot access the from main, which is located at root.
For some unknown reason from . syntax only shows me files/folders I have created myself.
I'm trying to access build module where there is a class TFNET which I want to import
from darkflow-master.darkflow.net.build import TFNET
Downloading directories like this isn't how you'd typically include external dependencies in a python project.
A more conventional approaches would be ot install the project using pip, it looks like darkflow gives information on how to do this.
Alternatively, just ensure the libraries are in your PYTHONPATH, it looks like pycharm has a way to do this: PyCharm and PYTHONPATH
Reading between the lines, it sounds like you have downloaded two libraries from github, and copied them into your project.
Python needs to know where to find source files.
If you want do it this way, you need to tell your python environment where to find the new sources. Pycharm looks after python environments while you are in python.
Please see https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
but this won't tell python outside of Pycharm where to find your library source.
pip is most likely the answer. pip can install globally or inside python virtual environments, in either case, it puts the library code in a location python is expecting to find it.
On this point, please learn about python virtual environments. These are self-contained python mini-worlds. In a venv, you can run a specific version of python with specific packages. Pycharm works well with them, it is easy to set up virtual envs with pycharm. When you are 'inside' a venv, pip will install into the venv, therefore not touching your system python or the python of any other projects.
Also, pip normally installs from an official repository (pypi) but you can tell it to use a git repository as the source of your install. Normally people who write libraries send their mature versions to pypi so it is unusual to fetch from a git repository, but if you want the very latest version, or if the author has not published the library, it's an option.
Note that pip doesn't work with any arbitrary python code. It must be set up to be seen by pip as a python package.

How to install a python program using setup.py

I am trying to install labelImg using setup.py. I run the command:
sudo python3 setup.py install
to install it and everything seemed to be fine. Unfortunately when I tried to execute the program (just tried labelImg &) got an import error:
ImportError: No module named 'resources'
So, I was wondering if I did something wrong or if there something I could do to fix it. My first thought is to provide an absolute import path to resources (and to the following libs imports) but that does not seem the right thing to do. Also it might work for a small project but it's obvious out of reach for a big one.
The git repo seem to imply that I should run it via python, but why then a setup.py exists?
I know I can use the program via python or even install it via PyPI but I am not interested in that.
The labelImg project depends on other libraries to work such as pyqt5-dev-tools and lxml.
If you check their documentation, first you have to install pyqt5-dev-tools:
sudo apt-get install pyqt5-dev-tools
Then install lxml:
sudo pip3 install lxml
After that you have to run the make command in order to build the pyqt5-dev-tools library so that the python code can use it properly (make is used to build executable libraries and programs from source code):
make qt5py3
And finally you can run python3 labelImg.py and use labelImg.

Conflicts when installing Anaconda Python

I have recently installed the Anaconda distribution of Python. I then inserted the following line into my .bashrc file:
export PATH=/home/karnivaurus/Libraries/Anaconda/bin:$PATH
So, there are now two python binary files: one in /usr/bin/, and one in /home/karnivaurus/Libraries/Anaconda/bin.
I also have a python script, which attempts to import a module named caffe, with the line import caffe. Now, if I run python caffe from the terminal, the script runs fine. However, if I open the script in PyCharm, and set the interpreter to be /home/karnivaurus/Libraries/Anaconda/bin/python, I get the following error:
ImportError: No module named caffe
Based on all this, I have two questions....
If I run the python command from the terminal, which binary file would it execute? The one in /usr/bin, or the one in /home/karnivaurus/Libraries/Anaconda/bin? My intuition is that it runs the first one, due to the discrepancy in behaviour with PyCharm. In that case, how can I force my system to use the Anaconda version?
If I install a new package, for example pip install caffe, then where will it be installed to? Will it be installed to /usr/local/lib/python2.7/site-packages, or to /home/karnivaurus/Libraries/Anaconda/pkgs? How can I be sure that my python command will know where to find the new package?
Thank you!
Answer to 1:
Based on your example: export PATH=/home/karnivaurus/Libraries/Anaconda/bin:$PATH the /home/karnivaurus/Libraries/Anaconda/bin comes first, so the python from there should be the one to be executed.
But the definite answer depends on result of running: which python.
Answer to 2:
In Anaconda, use conda instead of pip to install packages. When you install using pip install caffe you'll be installing to /usr/local/lib/python2.7/site-packages.
Use conda install caffe to install to /home/karnivaurus/Libraries/Anaconda/pkgs.
Above two answers explain why even if you pip install spam package, python would say ImportError: No module named spam. Essentially you install to ordinary Python, but you attempt to import in Anaconda's python.

Best Practices in Installing Python Modules on Windows

I'm trying to install openpyxl and I haven't the clue how to install it. I have my C:\Python27...but what directory do I put it in? FYI, I'm a complete noob to modules and what not...I appreciate your help.
I have put in the lib and libs directory of python and try installing it to no avail. Here is a link to the site I got my download... http://pythonhosted.org/openpyxl/.
Basically, I extracted the file and move it to the lib directory. However, I cannot import it.
There is a setup.py script provided. Typically you run:
python setup.py install
From the directory were you've downloaded the library, and it gets installed automatically.
Consider also more convenient tools that manage installing Python libraries in general like pip or easy_install mentioned in other answers and comments.
It's even easier when the package is on the Python Package Index (PyPI). Just install pip and use it to download and install the package and all its dependencies.
https://pypi.python.org/pypi/pip

Using pip to install single-file python modules

I'm wondering if there's a way to "install" single-file python modules using pip (i.e. just have pip download the specified version of the file and copy it to site-packages).
I have a Django project that uses several 3rd-party modules which aren't proper distributions (django-thumbs and a couple others) and I want to pip freeze everything so the project can be easily installed elsewhere. I've tried just doing
pip install git+https://github.com/path/to/file.git
(and tried with the -e tag too) but pip complains that there's no setup.py file.
Edit: I should have mentioned - the reason I want to do this is so I can include the required module in a requirements.txt file, to make setting up the project on a new machine or new virtualenv easier.
pip requires a valid setup.py to install a python package. By definition every python package has a setup.py... What you are trying to install isn't a package but rather a single file module... what's wrong with doing something like:
git clone git+https://github.com/path/to/file.git /path/to/python/install/lib
I don't quite understand the logic behind wanting to install something that isn't a package with a package manager...

Categories

Resources