I've got a python package which requires to compile and install executable written in C (it is helper program and acts as a script), which will need to go into proper place, i.e. same place where scripts would go ($VIRTUAL_ENV/bin, /usr/local/bin, etc).
What would be the correct way to implement this?
I found an example, but it works only when using pip to install the package, but fail when I try to use python setup.py install: they will build the executable, but will not install it during the "install bdist_egg" stage.
Related
After using macports pip-2.7 to install pyflakes, I can run it manually from the pip installation directory using the command line like:
python /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyflakes myfile.py
However, I'm trying to get my mac emacs environment setup with elpy, and flymake seems to not be able to find pyflakes, giving me a dialog when I open up a python file in elpy mode saying:
Flymake: Failed to launch syntax check process 'pyflakes' (with args myfile.py): Searching for program: no such file or directory, pyflakes. Flymake will be switched off.
I could go into /opt/local/bin and try to write my own executable file that runs the above python command. But that seems hacky and there must be a proper way to install/setup pyflakes such that flymake can find the command without manually creating wrapper scripts, isn't there?
I can run it manually from the pip installation directory using the command line like:
python /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyflakes myfile.py
Unless this directory is in your $PATH, Emacs has no way to know where to find pyflakes.
One option would be to create a symbolic link, e.g.
ln -s /opt/local/.../python2.7/site-packages/pyflakes /opt/local/bin/pyflakes
Of course, you will have to use the full path. I shortened it for readability. This is relatively common on Linux machines, though I don't know how broadly it is used on OSX.
Another option, one which I prefer, would be to reinstall pyflakes using pip install's --user option:
--user
Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%Python on Windows. (See the Python documentation for site.USER_BASE for full details.)
--user is great for installing things that you might want to run independently of any particular project, like pyflakes. It installs things to your home directory instead of to a system directory, so you don't need any elevated privileges to use it.
You may have to add the user location to your $PATH variable, but you'll only have to do this once. Any future tools installed using pip install --user will become available immediately.
On my Linux machine, the directory that I had to add to my $PATH is ~/.local/bin/.
I am running Windows and am a beginner python user trying to install a few modules to run a python script. I have Python 2.7.9 and 3.4.2 both installed to the C:\ directory. I downloaded matplotlib-1.4.3.win-amd64-py3.4.exe and the corresponding .exe for python 2.7 from the Matplotlib website, but when I run the py3.4 exe the program cannot find Python 3.4 to update (Cannot install: Python version 3.4 is required, which is not found in the registry).
Python 2.7 installer works perfectly. Is there a misset PATH variable in Windows I can modify so the .exe can function properly? In CMD 'Python --version' returns Python 3.4, so unsure how to fix the issue. I installed these months ago, and may have put them in Downloads before transferring both to C:\ for clarity, which may be the problem but am unsure how to fix it.
Also, if your answer involves pip in any way please clarify how exactly to use pip in Windows. A lot of websites say to run eg. 'pip setup.py install' in the 'terminal' but do not specify if they mean Windows CMD terminal, IDLE GUI, or Python.exe command-line interface. Thanks a lot!
Not the answer to your actual question, but some clarification on your last point:
but do not specify if they mean Windows CMD terminal, IDLE GUI, or
Python.exe command-line interface.
Yes, this requires to know some context that a beginner may not have. The command pip is always used in the CMD terminal. So open CMD, and enter
pip3 install matplotlib
Notes:
Use pip3 when installing for Python 3. Then you're certain you're not accidentally installing libraries for Python 2.
pip setup.py install does not exist. You're mixing up two mechanisms to install Python packages/libraries:
One uses pip, with aforementioned pip3 install <something>. Pip goes looking online, finds a corresponding package name in a database, retrieves the URL for that package, downloads the package and installs the package. All in one command.
python3 setup.py install (again explicitly use python3 or python2 to be sure) requires you to find the package, download it, unzip it, and then in the CMD terminal, inside the unzipped folder, run the python3 setup.py install command.
This second method is usually for the latest-greatest version of a package that is not yet in pip's database, or for packages that never were in pip's database in the first place.
Generally, as a beginner, you want to stick with pip. If you ever run into the issue with the package not being available via pip, you may still be able to use pip for downloading and installing, like for example so:
pip install https://github.com/matplotlib/matplotlib/archive/master.zip
which would install the most recent matplotlib (which won't have even a version number yet, so bugs could be around).
All of these commands happen in the CMD terminal: downloading/installing packages generally all go through the terminal.
Also, when people mention "terminal", they will mean (for Windows) something like the CMD terminal. When it has to be done inside Python, it is generally called the "Python prompt". (IDLE is yet a different beast, that I'm not familiar with. I'm guessing that it has several parts, including a text editor section and a Python prompt section.)
I'd like to make a switch from Windows to Linux (Ubuntu) writing my python programs but I just can't get things to work. Here's the problem: I can see that there are quite the number of modules pre-installed (like numpy, pandas, matplotlib, etc.) in Ubuntu. They sit nicely in the /host/Python27/Lib/site-packages directory. But when I write a test python script and try to execute it, it gives me an ImportError whenever I try to import a module (for instance import numpy as np gives me ImportError: No module named numpy). When I type which python in the commandline I get the /usr/bin/python path. I think I might need to change things related to the python path, but I don't know how to do that.
You can use the following command in your terminal to see what folders are in your PYTHONPATH.
python -c "import sys, pprint; pprint.pprint(sys.path)"
I'm guessing /host/Python27/Lib/site-packages wont be in there (it doesn't sound like a normal python path. How did you install these packages?).
If you want to add folders to your PYTHONPATH then use the following:
export PYTHONPATH=$PYTHONPATH:/host/Python27/Lib/site-packages
Personally here are some recommendations for developing with Python:
Use virtualenv. It is a very powerful tool that creates sandboxed python environments so you can install modules and keep them separate from the main interpreter.
Use pip - When you've created a virtualenv, and activated it you can use pip install to install packages for you. e.g. pip install numpy will install numpy into your virtual environment and will be accessible from only this virtualenv. This means you can also install different versions for testing etc. Very powerful. I would recommend using pip to install your python packages over using ubuntu apt-get install as you are more likely to get the newer versions of modules (apt-get relies on someone packaging the latest versions of your python libraries and may not be available for as many libraries as pip).
When writing python scripts that you will make executable (chmod +x my_python_script.py) make sure you put #!/usr/bin/env python at the top as this will pick up the python interpreter in your virtual environment. If you don't (and put #!/usr/bin/python) then running ./my_python_script.py will always use the system python interpreter.
/host/Python27/Lib/site-packages is not a default python directory on linux installations as far as I am aware.
The normal python installation (and python packages) should be found under /usr/lib or /usr/lib64 depending on your processor architecture.
If you want to check where python is searching in addition to these directories you can use a terminal with the following command:
echo $PYTHONPATH
If the /host/Python27/Lib/site-packages path is not listed, attempt to use the following command and try it again:
export PYTHONPATH=$PYTHONPATH:host/Python27/Lib/site-packages
If this should work and you do not want to write this in a terminal every time you want to use these packages, simply put it into a file called .bashrc in your home folder (normally /home/<username>).
When installing other python libraries, specify the pip version you want to install it to, if it's python2 you use, then enter this syntax:
pip2 install <package>
For python3
pip3 install <package>
Background
Windows does not include a compiler by default, and installing a compiler (and perhaps configuring Python to use it) is a complicated enough task that many developers avoid doing so. To this end, many packages that have binary dependencies are available as a precompiled Windows executable that contains binary files. As an example, there is psycopg.
The executable file is an installer. When executed, it provides a graphical interface that locates an installed version of Python via the registry, and it installs the Python library and the included binary dependencies in the global Python installation.
However, this is not always desirable. Particularly when using virtualenv, developers don't want to install the library globally. They want the library installed in the virtual environment. Since this environment is not represented in the registry, the graphical installer cannot locate it. Luckily, a command similar to the following can be used to install the library to the virtual environment:
C:\> C:\virtualenv\Scripts\activate.bat
(virtualenv) C:\> easy_install psycopg2-2.5.win32-py2.7-pg9.2.4-release.exe
Note that this works regardless of whether easy_install comes from setuptools or distribute.
The actual question
Why does this command work? What is it about the exe that allows easy_install to process it?
I have noticed that the exe seems to be some kind of zip file. 7-Zip is able to open it for browsing, and these exe files that easy_install can process seem to have a common file structure. They have a top directory named PLATLIB which contains an egg-info file or folder and another (possibly more than 1?) folder. Are these exes just Python eggs with some kind of executable wrapped around them? How might I produce one myself? (Or to word it differently, is there some standard way of producing exes like this?)
Edit
Bonus question: Why doesn't pip work with these files?
What are the differences between the below commands
python setup.py install develop
Doesn't work for me error No such file or directory: 'build/bdist.macosx-10.7-intel/egg/test-easy-install-37886.pth'
python setup.py develop
Works for me appears to make an .egg link file
python setup.py install
Works for me appears to make a .egg file which in .zip file format
Develop is a setuptools / distribute feature that allows you to add a project
to your Python environment without installing it - so you can continue
its "development"
In other words, when you call "python setup.py develop", setuptools will
compile the metadata and hook your project into Python's site-package,
but the packages and modules that will be used are the one in the
directory where you've run that command.
This is useful to continue working on your code and testing it without
having to run "python setup.py install" on every run
With develop, Python 'pseudo-installs' a package by running the setup.py script instead of install. The difference is a modification of the environment (it doesn't with develop), so a package can be imported from it's current location instead of a site-package directory. The advantage of this is you can develop packages that are being used by other packages, and you can modify source code in place with develop.
As far as "setup.py install develop", I've never seen anyone use that before, sorry.
source
source
source
python setup.py install develop
Is a wrong command.
When you use develop you use the current code when you run your application.
When you useĀ install and then modify you code, your modifications will not be taken in account while running your app. until you rerun install or develop.