Using a python library (Biopython) from a python program on a different folder that is installed - python-3.x

I generally like to make my python programs in a text editor and then run them after they are complete instead of line arguments. Thus, I save those .py files on a convenient folder location instead of Python program files.
I then run my .py file using Command Prompt. However it has not worked for the Biopython library as import Bio gives back a Traceback No module named 'Bio'. However, using line arguments directly on python shows it is installed.
I have never had this issue with Python in general and other downloaded libraries (import numpy for example works fine). How do I make the files available to be open from any location?? Or how do I provide the path in import?
To clarify and add:
1) I use Python from Windows 10
2) I downloaded and installed Python(3.7) from python.com
3) I downloaded Biopython using pip (and all other libraries I downloaded)
4) I also tried it on Jupyter notebook and also does not work to import Bio, whereas Import numpy does.
Thanks!

Related

How do tell "swig -python -py3 myswig.i" not to include annotations

I need to use SWIG to support both Python 2.7 and Python 3.10. [Yes, I know that Python 2.7 is dead, and we're doing our best to migrate users away from it as quickly as we can.]
I generate my module via setuptools.setup with a Swig extension. I can run the setup using both Python2 and Python3. The setuptools program creates a separate shareable library for the Python2 and Python3 runs. However both runs generate a myswig.py file in the same location.
It turns out that the Py2 and Py3 generated files are identical, except that the Py3 generated file contains annotations for the functions and the Py2 doesn't. Python3 can read the Python2 generated file and works just fine. Python2 cannot read the Python3 generated file.
I've tried both %feature("autodoc", 0); and leaving out this line completely, and I still get annotations.
So is there someway of either:
Turning off annotations in the generated file
Adding from __future__ import annotations automatically to the generated file
Don't use -py3. It's not required for Python 3, but enables Python 3-specific code features like annotations.

Import a python module from within a packed module

So I builded a python package localy:
cgi#cgires:~$ pip list | grep mads
madscgi 0.1.0
Its nice! Afterwards I can use it in Jupyter Notebook, in iPython Shell, in Python Shell and even in python scripts outside the modules code. So it works as expected 100% outside the modules code:
Thats nice, but next I want to import code from one builded module (inside the package) into another python file (inside the package). Lets name it import_test.py and try it out:
So it fails if it is getting executed in the directory, where the package is build from. And it looks like, that the python interpreter is taking the parent directory (with the same name like the module) and this is failing.
Is is possible to enforce the usage of the installed pip-package?
As #MisterMiyagi pointed out, the problem was, that there were an upper folder which had the same name as the module.
Here: mads_cons is the upper folder from import_test.py. Therefore, the upper folder is getting imported instead of the via pip installed module. Thats it.
The file you want to import should either be in the same folder or referred to with the absolute path of it.
If that doesn't suit you, you can call sys.path
import sys
sys.path
You can keep your file in any of the directories sys.path returns.
Smart would be, if you keep the file inside.
......../site-packages/

Running python scripts from command line using different python versions defined in anaconda envs

I have 2 python tools that I have to run via the windows cmd line. One is written in python2.7 while the other requires python3.6.
I have installed the newest Anaconda python3.7 version and created two new environments in 'C:\ProgramData\Anaconda3\envs' called 'python27' and 'python36'. For some reason I had to manually install numpy and scipy using conda install -n env_name numpy scipy for each of the new environments.
The reason I have to run both tools using the windows cmd line is that I have integrated them into a workflow environment (RCE by the DLR in case this is relevant), which executes integrated tools in this way. Which means I cannot simply use the Anaconda Prompt instead.
I cannot simply add the python installation to the PATH environment variable because of each tool requiring a different python version (and the file being called 'python.exe' in all versions), so I tried to create aliases for the cmd prompt as suggested by "roryhewitt" in this thread Aliases in Windows command prompt.
my 'python27.bat' file:
#echo off
echo.
C:\ProgramData\Anaconda3\envs\python27\python.exe %*
The problem with this approach is that python encounters an error when trying to import numpy:
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\envs\python27\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import core
File "C:\ProgramData\Anaconda3\envs\python27\lib\site-packages\numpy\core\__init__.py", line 71, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
1. Check that you are using the Python you expect (you're using C:\ProgramData\Anaconda3\envs\python27\python.exe),
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy versions you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: DLL load failed: The specified module could not be found.
Does anyone know a better way to run python scripts with a specific python environment via the windows cmd line, or what is causing the import error when I use the alias?
TLDR: I have 2 python tools that require python2.7 and python3.6 respectively. I have to run these tools using the windows cmd line and using aliases to the 'python.exe' file in the specific anaconda environments results in an import error of numpy. Is there a better way to handle two python environments via the cmd line or an easy fix for the import error?
It seems that the ImportError when using python2.7 via the alias in the cmd line was due to the anaconda environment not being activated. This caused the module to be unable to load properly.
I have added the conda functionality to be used in the cmd line as instructed in the post by "Simba" here: Conda command is not recognized on Windows 10. This has fixed the numpy ImportError.
I can now execute the desired tools from the cmd line using the correct python version by activating the correct anaconda environment first, and then calling the alias
Example:
C:\Users\user>conda activate python27
(python27) C:\Users\user>python27 main.py

Where can i find the pyc file?

The python3 version is Python 3.5.3 in my os.
mkdir workspace
cd workspace
vim print.py
print("i am learning")
Saved and exit.
python3 print.py
i am learning
As far as i knew, python source file was parsed and compiled into pyc file when to execute it.
ls
print.py
There is no pyc file in workspace directory,where is the complied print.py file then?
sudo find / -name ".pyc"
The find command still can't search pyc file such as print.pyc .
python3 -m compileall can create the compiled file for print.py manually,where is the compiled file for print.py created by python itself?
Does python3 delete the print.pyc after executing python3 print.py?
Ok this is one big of a problem I ever had when I'm started to learn python few years back. Python is just like any other oop programming languages which does compilation before program execution. When python compiles its program, it creates the bite code which is you can see by standard library called dis.
import dis
print(dis.dis(your_program))
Sometimes (not always) python creates .pyc file for the running programs to improve the speed up the loading of import modules but not to improve the execution time. So hope you get intuition behind .pyc, furthermore .pyc only creates when your module is import by another module.
As an example, Imagine you have this print.py (Let's modify it shall we)
def return_print_statment(statement):
print('Printed version: ', statement)
Suppose this module imported by another custom module called views.py. In views.py there is a module_view which will use the return_print_statment
from print import return_print_statment
def module_view():
...
return_print_statment(output)
So in the compilation, since you have imported the print.py python will generate print.pyc file for it. In python 2.0 python will put the .pyc to right next to your program in the same folder, but in python3 instead of creating in the same folder python will create separate folder called __pycache__ in the same directory to put these byte codes.
python3 -m compileall .
To compile all .py files in your current directory.
http://effbot.org/pyfaq/how-do-i-create-a-pyc-file.htm

How to create a Python Executable that can run other python-files stored in a folder?

Is it possible to create a Python Executable file (using PyInstaller or similar) that can in its code access other Python-files stored in a specific folder?
The reason for that it MUST be an Executable is that the script sometimes must be run from computers that has not it's own Python installed. I have no problem creating an executable (with PyInstaller for example) and they work fine. The python script itself loads various kinds of data into a database. But everytime there is a new kind of data that has to be loaded into the database I have to recreate the hole exe-file. I'm looking for a way for the executable to access python-files (with Data Load Instructions) so that the actual pyton load-files can be altered instead of the exe-file.
I have tried to find a solution using this:
import os, time
from subprocess import call
for file in os.listdir('.'):
if file == 'loadInstructions.py':
call(['python', file])
print(file)
cwd = os.getcwd()
print(cwd)
input('To EXIT progran press ENTER.')
time.sleep(3)
It works running it from a python editor but when I turn this into an exe-file it does not. If I creat an exe-file with "call(['python', file])" commented out the rest works which I interpret that the exe-file can find the file in question but not run it.
I would be gratefule for any help.

Resources