How to write %install in .py file - python

I use the code
%install package ver. which output is [Conda package activated] in my code, but i want to put my function in .py file.
when i do that, error of saying this %install part is
SyntaxError: invalid syntax
I want to know, how should I put that in a .py file
I tried to create .py file without that code, but it does not work

It appears like %install is what is called a "magic command" or "Magic", which is a special code snippet some environments (e.g. Jupyter) provide to run commands outside of python. For example, here is the list of magics available in IPython.
As they are not part of the standard python interpreter, you cannot use them in normal .py scripts.
Instead, you can call for example conda install <package> or pip install <package> from the command line (your OS command line, not an interactive python shell!) to install the package.
As a workaround, you can call the command line from inside python:
import os
os.system("conda install <package> -y")

Related

Python Package on GitHub

I made a python (3) package and I have been trying to upload it on Github. I also know how to push and install a git using pip. To test if it works as anticipated, I made a virtual environment on my local computer (linux) and pip installed my already pushed private package in there without a problem.
The issue is that I don't know how to access it!!! (I know how to activate and use virtualenvs; I don't know how to call my package) My package has a main interface that one would need to call it in terminal as follows:
python3 myui.py some_args *.data
and it's supposed to create some files where it's called. In other words, it's not exactly a module like numpy to be imported. I have watched/read many tutorials and documentations on the web and tbh I'm lost here.
You are looking for the -m flag. If you installed everything correctly, then the following command should allow you to run your script (based on your example). Note that you shouldn't add the file extension '.py'.
python3 -m myui some args *.data
If you have an actual package (directory with __init__.py file and more) instead of a module (a single .py file), then you can add a __main__.py file to that package. Python will execute this script when you use the -m flag with the package's name, in the same way as shown above.
python3 -m mypackage some args *.data
If you want to run a different script that is nested somewhere inside of that package, you can still run it by specifying its module name:
python3 -m mypackage.subpackage.myscript some args *.data
Another common way to make your script available uses the setup script (setup.py) or setup configuration file (setup.cfg) that is used to install the module or package. In that case, you can add an entry point to map a command to a specific module/function/etc. (as described in this Python packaging tutorial) so that you can run that command instead of having to use the -m flag with Python.
$ mycommand some args *.data

Running python in terminal without suffix

I feel like this is incredibly easy to fix, but for some reason it isn't.
I want to run a program in linux that opens python file filename.py by writing:
python3 filename arg
but it only works if i write:
python3 filename.py arg
Is there an easy way to run it without adding the extension? And without removing the extension completely? I wouldn't have imagined this to be a problem at all, but here we are.
Thankful for help!
Your file is called filename.py, therefore you call it with python3 filename.py. If you want to call it with python3 filename, you'll need to rename the file.
The one thing you can do is call Python with the -m switch, which will try to import a module with that name, i.e. python3 -m filename. That should work without renaming the file.
Firstly, if you start your python script with the shebang, then you won't need to type 'python3' before the filename each time you wish to run the code.
`#!/usr/bin/env python3'
Secondly, if you create a setup.py file is the same directory. Then you can install your script locally using:
$ sudo pip3 install -e .
from within your directory.
You will then be able to run your script from anywhere within terminal using:
$ myscript args
More info on creating a setup file here: https://python-packaging-user-guide.readthedocs.io/tutorials/distributing-packages/#setup-py
and example setup file here: https://github.com/pypa/sampleproject/blob/master/setup.py

Script working in ipython but not from the command line

I have a script that functions from within ipython but when I try and run the same script from the command line I receive import errors for a local module that I am trying to import:
from helper_functions.email_from_server import send_email
Error:
ImportError: No module named helper_functions.email_from_server
This script imports from within Ipython without any issues.
Comparatively, I have code that runs without any issues within ipython I can run another script using the command:
run script.py
From the command line I can run the same script:
python /dir/script.py
However this python /dir/script.py doesn't work with the script with local imports (from above) and I can't figure out if its a pythonpath issue or some local env issue? I have been reading through stack to find it but haven't been able to thus far. It feels like its just around the corner
One attempted solution:
PYTHONPATH=/dir/ python /dir/script.py
EDIT (to help clarify):
I am using an anaconda distribution on a linux machine.
Mucking about with PYTHONPATH is a recipe for sadness. You can do it, but you shouldn't. The correct thing to do is install your package in your correct environment. If you don't know how to create a package here's a super simple example. There may be some differences in your path when running via ipython vs command line.
You can find out what the differences are by using sys.executable and sys.path:
import sys
print(sys.executable)
print(sys.path)
Run that from IPython, and then run that from the python on your command line. You will undoubtedly get two different results. Since you're running Anaconda, you want to follow their guide for installing non-conda packages to install the one that you build.
Though of course that assumes that you've got the anaconda python on your path - you can check that out with which python since you're on Linux.
I resolved it via creating a wrapper shell script. Ugly in that i'm exporting the python path each time, but it works.
#!/bin/bash
export PYTHONPATH="${PYTHONPATH}:/my/dir"
source ~/.bash_profile
cd /my/dir && my/anaconda/location/bin/python /my/dir/to/script/cript.py

Can't call py.test <module_name>.py from command line?

I'm using Python 3.3 on Win7, and I'm fairly new to testing and py.test.
I have a simple test to run, and although I can run it from the command line by calling
$ python -m pytest testing.py
when trying to call it with the simpler line
$ py.test testing.py
it returns:
'py.test' is not recognized as an internal or external command,
operable program, or batch file
Do I need to have the py.test source folder in the same location as my program, or am I doing something incorrectly?
The system is telling you:
py.test is not installed in a standard location. If you installed using pip or easy-install, it should be in /usr/local/bin or possibly /opt/bin depending on your flavour of Linux, and these should be on your path.
py.test has not been marked as executable.
You installed from source using setup.py and the directory containing py.test is not on your path, probably in or below your home directory.
Check for these possibilities, correct them if necessary and post the results of your efforts.
Above one is true.
Try to create a new virtual environment and do a new py.test
And check your python installation and path if the executable is accessible by terminal
pip install pytest==6.2.1
It worked for me.
pip install pytest== (any pytest version number)

How to run Python egg files directly without installing them?

Is it possible to run Python egg files directly as you can run jar files with Java?
For example, with Java you might dos something like:
$ java -jar jar-file
A python egg is a "a single-file importable distribution format". Which is typically a python package.
You can import the package in the egg as long as you know it's name and it's in your path.
You can execute a package using the "-m" option and the package name.
However, python packages generally do not do anything when executed, and you may get an error. The -c option can be used to run code. (See http://docs.python.org/using/cmdline.html for details on command line options)
> python -m sphinx
sphinx is a package and cannot be directly executed
> python -c "import <package in an egg>; <function>();"
> python -c "import sphinx; print sphinx.package_dir"
C:\Python26\lib\site-packages\sphinx-0.6.1-py2.6.egg\sphinx
As of Python 2.6, you can use python some.egg and it will be executed if it includes a module named __main__.
For earlier versions of Python, you can use PYTHONPATH=some.egg python -m some module, and somemodule from the egg will be run as the main module. (Note: if you're on Windows, you'd need to do a separate SET PYTHONPATH=some.egg.)
For example, if you want to import the suds module which is available as .egg file:
egg_path='/home/shahid/suds_2.4.egg'
sys.path.append(egg_path)
import suds
#... rest of code
Python Egg file direct execution steps
Suppose if you have egg file and driver file to run through below command.
PYTHONPATH=eggfilename.egg python driverfile.py
above command for without install egg file with python code.

Categories