I've been using the Enthought Python Distribution (the Academic version) for some time now, but have been trying to install some extra packages for a project, and running into problems born of my relative inexperience with the command line.
These are:
COPASI
libSBML
StochPy
The last one went well, a simple python setup.py install and everything seems to be running fine. But neither COPASI nor libSBML seem to be working.
Importing either one of them is netting "ImportError: No module named COPASI/libsbml".
I installed libSBML according to the directions here.And used this suggested workaround to get it working with Enthought, to no avail. For COPASI, I installed COPASI as directed, and followed the following directions for the Python bindings:
Once you downloaded the binary package for the Java bindings you have to unpack it. It will be unpacked to a directory called copasi_python. This directory contains the native library, a python file, a documentation file and the license file. It also contains a directory called unittests with lots of unittests that can be used to check if the bindings are working.
To run the unittests, you first have to set the PYTHONPATH environment variable to the directory where the native library and the COPASI.py file are located. Once you changed into the unittests directory you find a file called runTests.py which runs all the unittests in the directory.
My edited .bash_profile file now looks like this:
export PATH="/Library/Frameworks/EPD64.framework/Versions/Current/bin:${PATH}"
export PYTHONPATH=/usr/local/lib/python2.7/site-packages
export PYTHONPATH=/Applications/COPASI/copasi35_python27_macosx107_x64:$PYTHONPATH
The first line is to make EPD the default python interpreter, the second is the result of the suggested workaround above, and the third is per the instructions for COPASI. Any idea what I'm doing wrong?
Related
I made a program in python which I now wanted to turn into an .exe so that other people at my office, who do not have python or any python skills can use it too. This is not the first time I did this, and I am still working on the same machine, however this time I run into the "module not found" error when trying to execute the exe.
Basically I created a GUI with PySimpleGUI and then followed my own guide from last time, where I created a spec file with pyi-makespec, specifying the paths in which the packages are located. These are two locations: in C:// where python is installed, and in the "venv" folder of my Pycharm project. PYSimpleGUI is located in "venv" but not in "C".
After creating the spec file I create the exe with pyinstaller. It was suggested to use --hidden-import=PySimpleGUI as additional flag, which I did do, but I still got the same error. I also made sure python is added to my PATH, but maybe I did something wrong there? Tha path I used is the one where python is installed: C:\Users\Username\AppData\Local\Programs\Python\Python39\Lib\site-packages is that right?
I am completely stuck and can't find any helpful information. How do I solve this issue? Also important: I do not have admin rights on my computer, so installing stuff is always linked to me having to call the support desk...
If more information about this project is needed, let me know.
Usually, modules not found tend to be the issues with virtual environment. Python uses those venvs as some sort of folders where it stores the Python interpreter and packages installed with pip. If you have an error saying some module is not found, you should either install it on your current environment or switch to the one where ot is already installed. For more details, I recommend you to take a look at this tutorial how to use and setup them.
Additionally, you do not need to compile Python code if you can afford relying on the interpreter installed. If you know your target audience has the interpreter installed (an interpreter is usually shipped with various Linux distributives), then you can just give them your source code or even pycache (.pyc) files that it can interpret as well.
I tried three different things at the same time, one of them seemed to have solved the problem:
1.) update pip with the following commands in the PyCharm Terminal
pip install --upgrade PySimpleGUI
2.) update PySimpleGUI with the following commands in the PyCharm Terminal
C:\Users\Username\AppData\Local\Programs\Python\Python39\python.exe -m pip install --upgrade pip
3.) add python39 to PATH
I checked again and I did have \Python39\Scripts in my PATH, but not \Python39 itself, so I added it. The way to do it on windows is to search for "environment variables" (Umgegungsvariablen in german), edit the "Path" Variable (double click or click and press edit, which opens a new window), then add new Path (mine is C:\Users\Username\AppData\Local\Programs\Python\Python39)
I sort them with Python39 being above Python39\Scripts.
This solved my problem with PySimpleGUI, my program started as expected, but then ran into another missing module error (xlsxwriter not found), which - fair enough - was not imported. (interestingly when running the code in pycharm I didn't need xlsxwriter). I imported it, added it to venv, and when starting the program again, the error showed up even before showing the GUI. I then upgraded xlsxwriter with
pip install --upgrade xlsxwriter
I again created the .spec file and ran pyinstaller to create the exe. This time it worked.
Now I only have issues left which are not connected to missing modules (just variable referenced before assigned).
I have the following problem:
I have to run some test/diagnostic Python script on a Windows system. Due to explicit requirement, the system has no default system-wide Python instance, but there are two different Python instances installed, used locally by applications running on the system. However, both these instances lack some basic modules my script uses (like logging, urllib, configparser etc.).
I want to run %PYTHONPATH%\python.exe myscript.py where %PYTHONPATH% points to one of the installed Python instances, but install the required additional modules "somewhere" outside %PYTHONPATH% (preferrably, in the same directory where my script is installed) so that my script can use them.
As my script is a test tool, it should not modify the OS or installed software, so the Python installation under %PYTHONPATH% should not be changed in any way.
It is also expected that the installation can be fully automated, ie. the best way to install would be just have the modules in the same .zip file with my script which is unpacked onto the target path.
It is also important that the system has no Internet access, so I have to download required files on another machine and copy them to the target system.
Can you guide me how to do it?
I found an answer myself - it is quite simple:
obtain the zip file containing standard modules from the appropriate Python version distribution (in my case it was the file python38.zip, it is inside the main zip file downloadable from Python site)
Unpack the contents of this file to c:\mydir\Python38\site-packages, where c:\mydir is the directory containing my script
set the environment variable PYTHONUSERBASE=c:\mydir before running my script
Now I can run the script and it finds all "missing" standard modules in c:\mydir\Python38\site-packages.
I think what youy are seeking for is a python virtual environment.
( internet needed )
Check :
https://docs.python.org/fr/3/library/venv.html
Then for the installation, you can creat a .exe file containing all dependencies.
(no internet needed)
Check:
https://www.pyinstaller.org/
I have downloaded the rpy2 package using MacPorts, but every time I try to import it using my IDE (Enthought Canopy version 1.5.4), I get the following error:
ImportError: No module named rpy2
However, when I run Python in my terminal and run import rpy2 there, it imports without any issue.
I did some googling on the issue, and it appears that Python's environmental variables in the terminal are not the same as the Python's environmental variables in the IDE, particularly the PYTHONPATH. I checked my PYTHONPATH in my terminal by running import sys; sys.path and got the following file paths:
/usr/local/lib/python2.7/site-packages
/Users/bob
/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Users/bob/Library/Python/2.7/lib/python/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rpy2-2.8.6-py2.7-macosx-10.6-intel.egg
The very last file path points to the location of the module that won't be imported in my IDE.
I then ran the same commands on a script in my IDE and got the following file paths:
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python27.zip
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/plat-darwin
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/plat-mac
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/plat-mac/lib-scriptpackages
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/lib-tk
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/lib-old
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/lib-dynload
/Users/bob/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/Users/bob/Library/Python/2.7/lib/python/site-packages
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/IPython/extensions
/Users/bob/.ipython
The location of rpy2 module is not present in the above block of file paths. So, it seemed logical to me to simply add its file path to the PYTHONPATH environmental variable.
However, an answer to this post (Using MacPorts to install modules via a certain path) mentions that it is potentially dangerous for the following command to be carried out:
sys.path.insert(-1, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rpy2-2.8.6-py2.7-macosx-10.6-intel.egg')
Can anybody support/gainsay this claim? Any additional explanation would be most helpful.
UPDATE
So, it turns out, it actually is BAD for the script. I decided to try running some examples from the tutorials, and the kernel just crashes every time, and must be restarted. How can I remedy this?
SECOND UPDATE
After following the advice set out in How to: Macports select python , I decided to run the following command in the terminal in order for MacPorts to select the version of Python that was being used by my Canopy IDE:
sudo port select --set python python27
This seems to have solved the issue, as my kernel does not crash anymore when I run basic commands from the rpy2 module. But I remain skeptical as to whether this is a long term solution or not, as I am still confused behind the discrepancies of installing a module via MacPorts versus the IDE's package manager.
I have downloaded a python package to install, on my ubuntu machine. The package has already a setup.py file to use, but I want to change the default python installation address to something else, for this package specifically (and not for good). So what I tried is:
First in the terminal, I export that address of the new folder:
export PYTHONPATH=${PYTHONPATH}:${HOME}/Documents/testfolder/lib/python2.7/site-packages
Then I add this exported address as prefix to the installation command:
python setup.py install --prefix=~/Documents/testfolder
The installation goes through. Now to make python always look for this new path as well (next to the default installation path), I export the address in bashrc file:
export PYTHONPATH="${PYTHONPATH}:~/Documents/testfolder/lib/python2.7/site-packages"
But now whenever I open a terminal and try to import the installed package, it cannot see ("no module named..."). Only when I open a terminal in the folder where I had the installation files (namely setup.py), and run python, can it then see the package, and it works there.
Why isn't my export in bashrc making the package available from anywhere?
Is there something I have done wrong in the above?
To answer your question about the export path. Do you have $PYTHONPATH as a part of your $PATH? If not you should add it to path.
The best way to handle this scenario in my opinion is to use a virtual python environment. There are a couple to choose from, but I like virtualenv the best. The reason to take this approach is because you can manage different versions of python in separate folders. And have separate packages installed in these folders. I recommend looking into it as it is a very useful tool. If you want an examole of how to use it i can provide that https://virtualenv.pypa.io/en/stable/
I recently had a problem with importing a python and therefore posted this question: Cant seem to import specific module in Python
Based on input I got I did some digging and saw the following:
If I open cygwin and input python --version, I get this
Marc#Marc ~
$ python --version
Python 2.7.12 :: Anaconda 4.2.0 (64-bit)
And if I open python using myCharm and input this:
import sys
print('\n'.join(sys.path))
I get:
C:\Users\Marc\Anaconda3\python.exe C:/Users/Marc/PycharmProjects/clustering/testing.py
C:\Users\Marc\PycharmProjects\clustering
C:\Users\Marc\PycharmProjects\clustering
C:\Users\Marc\Anaconda3\python35.zip
C:\Users\Marc\Anaconda3\DLLs
C:\Users\Marc\Anaconda3\lib
C:\Users\Marc\Anaconda3
C:\Users\Marc\Anaconda3\lib\site-packages
C:\Users\Marc\Anaconda3\lib\site-packages\Sphinx-1.4.6-py3.5.egg
C:\Users\Marc\Anaconda3\lib\site-packages\win32
C:\Users\Marc\Anaconda3\lib\site-packages\win32\lib
C:\Users\Marc\Anaconda3\lib\site-packages\Pythonwin
C:\Users\Marc\Anaconda3\lib\site-packages\setuptools-27.2.0-py3.5.egg
So assume this highlights the problem. Could anybody tell me what is wise to do here? Change the path in my terminal of change the path in python
If you set an envirnment variable called PYTHONPATH to a colon-separated (Windows: semicolon-separated) list of directories, each of them will be added to the interpreter's sys.path. This is the recommended way of making modules available.
So for example, let's suppose you had added Python modules in the directory C:\Documents and Settings\frits\private. Before you run your Python program you would execute the command
SET PYTHONPATH=C:\Documents and Settings\frits\private
No semicolon is required in this case because there's only one directory.
The when you run Python you will find that your sys.path has another entry corresponding to the C:\Documents and Settings\frits\private directory.
In the longer term, to avoid conflicts between the requirements of different projects (e.g. you want to run two programs that require a different version of the same third-party module) you should investigate the use of virtual environments.