I have tried nearly all solutions in my mind to do this, but the fact is I can't run a python script with modules imported already.
Here's my code for the module cls.py:
import os
def cls():
os.system('cls')
Given below is the code for opening python in cmd:
#echo off
python
pause
What I need is to open the python in the command prompt with the module cls imported. Also, when I try python -m <module> way, it doesn't show any errors, but the program ends.
Any help would be greatly appreciated and thanks in advance.
Saw this question related to mine, but it is not my problem: Run python script that imports modules with batch file
I think what you'r looking for is the interactive mode:
-i
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command
So just use python -i cls.py in your batch file. This would import your Python file and stay in the Python interpreter prompt. You could then just call cls() because it's already imported.
Alternatively, you could set the environment variable PYTHONSTARTUP in your batch file:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.
Related
More Generally: How can I run a process that runs from the terminal with required keyword arguments using python?
It seems subprocess.Popen is what I want to use.
Here is what I try to run from inside a script:
from subprocess import Popen
Popen(['goodreads-user-scraper', '--user_id 149832357'])
“goodreads-user-scraper” is recognized, but I don’t understand how to pass a keyword argument. I know how to pass an argument, but if I remove “—user_id” I still get same problem.
I have installed goodreads-user-scraper. It works well from the terminal. It is a python package that you can run from the terminal. Repository found here.
Below is how I would ideally like to run the process but from inside python:
pip install goodreads-user-scraper
goodreads-user-scraper --user_id <your id> --output_dir goodreads-data
Result of running from terminal:
Research
In my research I have found these two that seem helpful, but I haven’t been able to adapt the answers to my needs:
Why does passing variables to subprocess.Popen not work despite passing a list of arguments?
How to use subprocess popen Python
Separate all of your arguments like this:
from subprocess import Popen
Popen(['goodreads-user-scraper', '--user_id', '149832357'])
I want to convert the following sample python program into an executable file:
import os
print(os.getcwd())
To convert it into an executable I have used Pyinstaller:
pyinstaller app.py --onefile
And the EXE file is getting generated in the dist folder, but when I run it, it launches and immediately closes, and the expected print statement is not even displayed.
What could be the issue?
It does prints the statement, but just after printing it the code ends. You can add input() or use time.sleep(seconds) to make your program wait until you press a key or any particular number of seconds respectivly.
To check if your code(without the advice i have given) prints, start that python file in your command line.
I'm still a newbie at bash programming, but trying to run a program with little script. Reducing the problem to the error message, I have
cd /full/path/to/program
python3 -m krop
that is the command working when the actual folder is the /full/path/to/program
but if I run the same from root it doesn't work.
cd /another/path
python3 -m /full/path/to/program/krop
/usr/bin/python3: Error while finding module specification for '/full/path/to/program/krop'
(ModuleNotFoundError: No module named 'krop-0')
I tried lot of variants, but always the same output with errors. I do not have a clue of why the library "python3" adds the "-0" at the end of the name of the file.
What should I put to run the program from root?
python -m command expects a module name, similarly to the syntax you would import in a python program. So if your modules lies in ./directory and directory is a valid python module, you can do python -m directory.krop
You can't however index python modules from file system root. You have either to make your bash script run it in the good directory so you make a local import; or you have to package and install your module system-wide to make a global import that would be invoked with python -m krop from anywhere.
More information on packaging and installing modules: https://packaging.python.org/tutorials/packaging-projects/
Problem solved!,
It was a matter of managing the python import paths, as #hiroprotagonist replied. The list that contains all of directories python will use to search for modules, is available in a variable named sys.path.
So, if somebody wants to run a program (a 'library module as a script', according to python help) through python's command, from a directory different from the 'pwd' one, should write in the command line:
export PYTHONPATH='/full/path/to/program/'
python3 -c "import sys; print(sys.path)"
python3 -m krop
The second line is actually to print on screen, but the first one is the only necessary (export PYTHONPATH).
Thank you for the keywords and help!
Ps. May be should be edited the question title to "problem with a python command to run a program from command line on linux" or something like that.
Reference: python --help :)
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.
I'm a beginner of Python and I want to run a python3 script in PyCharm Python Console, but it raise an error below:
>>> /Users/mymac/Documents/Python/test.py
File "<input>", line1
/Users/mymac/Documents/Python/test.py
^
SyntaxError: invalid syntax
I don't know what's wrong the file path is, how can I solve the problem?
use execfile('/Users/mymac/Documents/Python/test.py'). You are trying to run a python file like an executable. On top of that you are doing it in python interpreter.
Use python's execfile routine to call a file from interpreter or use python test.py to run it from the terminal.
I recognized that the answer I accepted ahead wasn't a perfect method, cause function execfile has been deleted in Python3, the alternative method is using open like:
>>> with open ('/Users/mymac/Documents/Python/test.py', 'r') as f:
... exec(f.read())
...
Still thanks for guys who helped me!
This could be several things. Have you set your environmental correctly? You can test this by cmd: python it should return Python 3.6.4 if that is your current version. If not then head over to tutorialsPoint for how to correctly set up your path.
If that is correctly configured, then have you selected an interpreter in PyCharm. If not, File --> Settings --> Project: Network --> Project Interpreter. Select your python installation path.
Another thing to note is that I suspect you mean to use the terminal instead of the python console.
Then in PyCharms terminal section, python test.py.