Can't popen new instance of python.exe - python

There’s a project where I need to use both Python 3.3 and 2.7. I am trying to launch a script under Python 2.7 but it’s not working. Here is a simple example.
first.py
import subprocess
import sys
print('Inside first.py')
print(sys.version)
subprocess.Popen(["C:\Python27\ArcGISx6410.2\Python.exe", "second.py"])
second.py
import arcpy
print 'This is second.py'
This doesn’t work and the output is
Inside first.py
3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)]
File "C:\Python33\lib\site.py", line 173
file=sys.stderr)
^
SyntaxError: invalid syntax
That’s the entire stack trace. If I were to replace C:\...Python.exe with notepad.exe then it works. I’m using Liclipse on Windows 7.
UPDATE: it appears different versions of Python are run, when from the command line python first.py is 3.3 but py first.py or just first.py then 2.7 is used.

Try:
import os
subprocess.Popen(["C:\\Python27\\ArcGISx6410.2\\Python.exe", "second.py"], env=dict(os.environ, PYTHONHOME="C:\\Python27\\ArcGISx6410.2"))
Python on Windows needs a little help sometimes to figure out which version of the standard library to use.

Related

Installed Libraries are not being found by my IDE (Mac) [duplicate]

I am trying to import pyodbc module on a windows computer. It works in the terminal, but not the IDLE. The error message in IDLE is:
Traceback (most recent call last):
File "FilePath/Filename.py", line 3, in <module>
import pyodbc
ImportError: No module named pyodbc
This typically occurs when multiple versions of python are installed with different paths. You can check to see if you have multiple installations by opening up the IDLE terminal and using
import sys
sys.version
sys.path
These commands will print the system PATH and version of the current instance of python. Use this in both IDLE and the command line terminal to see where each differ. Once you know which version is the one you want then just remove the other. You could also remove all python instances and then reinstall a clean python environment but then you would have to re-install all of your modules using pip or easy_install
Open python in cmd (type python and press enter)
Import the module in cmd (type import modulename)
Type modulename.__file__
You will get the path where the module is stored
Copy the corresponding folder
In IDLE, import sys and typing sys.executable to get the paths where it looks for modules to import
Paste your module's folder in the path where IDLE looks for modules.
This method worked for me.
You can pip show after install package and know about location where package installed.
After that check in IDLE sys.path and if directory with package not in sys.path try to add it.
import sys
sys.path.append("/home/dm/.local/lib/python3.6/site-packages")
# or another folder that `pip show` about package.
this happen because of multiple python installed (32bit version, 64bit version) or 3v and 2.7v so to solve this problem you have to invoke the idle for that specific version like this
cd to the dir of the version that the import work fine in cmd in that folder type this command below
pythonw.exe Lib\idlelib\idle.pyw
this command will invoke idle for that version
and the import will work fine
Me too had the same issue while trying to import a module which was successfully imported on terminal and not able to install on IDLE.
How I fixed?
Assuming you know how to execute commands on terminal as well as inside of python interpreter
Open your Terminal & execute the below commands :
:~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import sys
>>> sys.version
'3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]'
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-
packages', '/usr/lib/python3/dist-packages']
>>>
Now import your module inside of your python3 interpreter.
>>> import nester
>>>
>>> nester.__file__
'/usr/local/lib/python3.6/dist-packages/nester.py'
>>>
Open your IDLE and run the below commands and compare them
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more
information.
>>> import sys
>>> sys.version
'3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit
(AMD64)]'
>>> sys.path
['','C:\Users\username\AppData\Local\Programs\Python\Python39\Lib\idlelib', 'C:\Users\username\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\username\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\username\AppData\Local\Programs\Python\Python39', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages']
>>> sys.executable
'C:\Users\username\AppData\Local\Programs\Python\Python39\pythonw.exe'
Now if you compare both outputs from Terminal & IDLE,
Terminal Module location is different from IDLE
I was using Ubuntu 18 terminal on windows machine
So I just copied my file to 'C' directory and ensured its file privileges. That's it.
:~$ cp -p /usr/local/lib/python3.6/dist-packages/nester.py /mnt/c/Users/username/AppData/Local/Programs/Python/Python39/Lib/
It worked!!
I Found the solution. It works for me
The problem is your installation directory does not match with the python version directory.
solution is >>>
type %localappdata% in your search bar then go to this folder.
here select the program folder. then select Programs , Python , Python version , Scripts
copy the location of the Scripts folder
open command prompt and type cd //yourpath (in my case cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts)
if you wanna install numpy , now run pip install numpy
When you put your python scripts that have import pandas in the same folder as the site packages like pandas for example and use the same version of python that is used on CMD, it should help run your scripts in IDLE.
Check the path of your code, and that of the module. Copying the module to the path where code is worked for me.
'sys.executable' will give the path where code is stored.
For windows, open command prompt and enter pip show pyodbc to get the path of package and copy the path.
then open idle and run these lines
import sys
sys.path
Match the path from command prompt and the paths mentioned in the list provided by running above lines in IDLE. If the path is not mentioned then run these lines in idle
sys.path.append("Enter the copied path of package here")
After executing these lines, check again by importing the package that if it works for you.

No module named "selenium/googletrans/tabula" when running script through cmd but works fine on idle?

I have a list of python scripts which run perfectly fine through idle. However, when I run it through cmd I get various errors stating
No module named 'selenium',
,No module named 'googletrans'
,No module named 'tabula'
When I use pip install it says.
'Requirement already satisfied'
Also when I check pip list the above mentioned modules are there.
sys.path
In cmd
['', 'C:\Users\jay.haran\Documents\PC_scrape\Scraper', 'C:\Users\jay.haran\ C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\Lib\idlelib', 'C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\python36.zip', 'C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\DLLs', 'C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\lib', 'C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32', 'C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\lib\site-packages']
'I added the second and third item in the list
In idle
['C:\Users\jay.haran\Documents\PC_scrape\Scraper', 'C:\Users\jay.haran\Documents\PC_scrape\Scraper', 'C:\Users\jay.haran\Documents\PC_scrape\Scraper\ C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\Lib\idlelib', 'C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\python36.zip', 'C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\DLLs', 'C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\lib', 'C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32', 'C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\lib\site-packages']
sys.version
In both
In cmd and In idle = 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)]
sys.executable
In cmd = C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\python.exe
In idle = C:\Users\jay.haran\AppData\Local\Programs\Python\Python36-32\pythonw.exe
There are other modules installed such as bs4, xlrd, pandas that aren't causing any issues.
Thank you very much for any help.

Python argparse behavior differs depending on launch mode (Windows)

This is a excerpt of my test-argparse.py piece of software:
from __future__ import print_function
import argparse
import sys
def manage_command_line():
parser = argparse.ArgumentParser(description='Simple Description')
parser.add_argument('code', type=str, help='Project code')
return parser.parse_args()
args = manage_command_line()
print(args)
print(sys.version)
When called as a parameter of the interpreter (result is correct and expected):
c:\Python27\python.exe test-argparse.py KB130
Namespace(code='KB130')
2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)]
However if call it directly, relying in windows file association, the result is completely different:
C:\Users\PGO\Documents\docauto>test-argparse.py KB130
usage: test-argparse.py [-h] code
test-argparse.py: error: too few arguments
The definition of Windows file group association and the redirection is standard and as described in Python 2.7.14 manual (chapter 3.3.4):
C:\Users\PGO\Documents\docauto>assoc .py
.py=Python.File
C:\Users\PGO\Documents\docauto>ftype Python.File
Python.File="C:\Python27\python.exe" "%1" %*
C:\Users\PGO\Documents\docauto>
And consistent with my system paths, although my Python version is 2.7.12 and not 2.7.14, which probably makes no difference at all.
Question: Is there a way for obtaning a consistent result in both cases? I would like a consistent behavior of the application independent of the way it is executed.
This is what I get:
C:\Users>ftype Python.File
Python.File="C:\Windows\py.exe" "%L" %*
Both cases work for me correctly. I have Python 3.6, that could explain the difference in executable name.

Can't run Python code in Command Prompt

I'm new to Python and I created my first piece of code which is simply
print('Hello World!')
and it works fine and I named it hello.py. When I try to open the file it says with "python hello.py"
I get an error message that says
File "hello.py", line 1
PYthon 3.4.4 (v3.3.3:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1.600 64 bit (AMD65)] on win32
SyntaxError: invalid syntax
I added Python to the PATH so I don't know what's the issue
You're not supposed to put those lines into your script. They come from the interpreter, giving information to you. You're only supposed to include in your script the commands that you want to give to the interpreter. In your case, your hello.py file should consist entirely of this:
print('Hello World!')
and nothing else.

Can't import the cx_Oracle module unless I'm using an interactive shell

When using Python on an interactive shell I'm able to import the cx_Oracle file with no problem. Ex:
me#server~/ $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
As you can see, importing works without a hitch. However, when I try to run a Python script doing the same thing, I get an error:
me#server~/ $ sudo script.py
Traceback (most recent call last):
File "/usr/local/bin/script.py", line 19, in <module>
import cx_Oracle
ImportError: No module named "cx_Oracle'
Here is the important section from script.py:
# 16 other lines above here
# Imports
import sys
import cx_Oracle
import psycopg2
...
I'm befuddled here. Other pertinent information is the server I'm running is Ubuntu 14.04.1 LTS (upgraded from 12.04) 64bit. which python and sudo which python both point to the same location. Also, doing this as root via sudo su - gets the same results; import OK from interactive but error from script.
Nothing other than the OS upgrade happened between when this worked and when it stopped working.
Sorry, all. This was a silly on my part. Turns out the script in question was using Python3, and when the server upgraded, Python3 went from being 3.2 version to being 3.4 version.
Once the cx_Oracle module was set up in the 3.4 version, everything worked as expected.
Phil, your final note talking about the shebang was what lead me to discover this, so kudos to you! The reason I didn't mark your response as the answer was because technically it wasn't but led me on the right path.
Cheers!
sudo starts a new bash environment which is then pointing to a different python executable (different installed modules).
You can verify this with which python and sudo which python
EDIT: so if they point to the same executable, then you should look at sys.path to find differences. In both environemnts you can:
python -c "import sys; print('\n'.join(sys.path))"
sudo python -c "import sys; print('\n'.join(sys.path))"
Look for differences. If there are none:
A common error in import situations like this is that python will first look at the local dir. So if you happen to be running python and importing something what is found locally (i.e. cx_Oracle is a subdir of your current location), you will get an import error if you change directories.
Final note: I have assumed here that the shbang of the script.py points to the same executable as which python. That is, that python script.py and script.py return the same error.

Categories