I have seen this post asked before in stackoverflow but it was 4 years ago (How to fix the python path) so I am not sure wether this is the right solution because I am using a newer version of python (3.5.2). This is what I see in a Python Shell:
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> os.environ['PYTHONPATH'].split(os.pathsep)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
os.environ['PYTHONPATH'].split(os.pathsep)
File "C:\Users\John\AppData\Local\Programs\Python\Python35\lib\os.py", line 725, in __getitem__
raise KeyError(key) from None
KeyError: 'PYTHONPATH'
I want to find the pythonpath.So, how can I fix this error?
There is no PYTHONPATH variable in your OS Environemental variables. Hence the error.
It is not created by Python installation (atleast in windows). You have to create one variable.
To check if there is one such environmental variable, type below command:
SET PYTHONPATH
You can also create and set it using below command
SETX PYTHONPATH <your desired path>
You may want to check sys.path
A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.
First of all make sure if you have any environment variable with this name. I also got an error like this. I used PATH instead of PYHONPATH and this worked for me. I am working on Ubuntu btw.
Related
Python 3.6.5
I am aware of this one: Why does my python not add current working directory to the path?
But the problem there is that he's doing something more complicated (referring to a sub-folder but executing from a main folder). The answers there are to either simplify things or to add package definitions.
And the selected answer even says: "It is the script's directory that is added"
However, my problem is really more simple: My script's directory ISN'T added.
Basically, all the tutorials on the internet say: import mymodule
When I do that, I get a name error...
My folder structure:
C:/Projects/interner
interner.py # this is the main program body
aux.py # this is the auxiliary file I would like to import into the above
I've tried both coding 'import aux' inside interner.py, and also using the interactive console:
cd c:/Projects/interner
python
import aux
To no avail (ModuleNotFoundError: No module named 'aux')
My sys.path:
['C:\\Tools\\Python\\python365\\python36.zip', 'C:\\Tools\\Python\\python365']
(both from inside the script and from interactive console)
Could you please tell me why I can't import local scripts? Is it because my sys.path is missing the PWD? If so, why is it missing it?
Edit: Doing this to help investigation:
>>> import os; print(os.listdir("."))
['aux.py', 'hw.py', 'interner.py', 'my_funcs.py']
I believe this is a Python bug, specific to the embeddable (ZIP file without an installer) Windows distribution. I’ve filed https://bugs.python.org/issue34841.
Removing the python37._pth file (presumably python36._pth in your case) from the distribution fixed it for me.
I don't know why but it seems that "" is missing from your sys.path variable, and that prevents from importing modules from current directory all right!
I can somehow reproduce your issue (eatcpu.py is in my current dir):
$ python.exe
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\Windows\\system32\\python27.zip', 'D:\\AppX64\\Python27\\DLLs', ... etc...]
>>> import eatcpu
works. Now in another python session:
$ python.exe
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.remove("")
>>> import eatcpu
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named eatcpu
>>>
So the quickfix for you is to do:
import sys
sys.path.append("")
It looks like you are using the embeddable distribution of CPython rather than one of the regular installers. As described on the documentation page:
The embedded distribution is a ZIP file containing a minimal Python environment. It is intended for acting as part of another application, rather than being directly accessed by end-users.
Since you seem to be directly accessing Python rather than embedding it, you should consider using the regular (or Microsoft Store) installer (also described on the page I linked above).
Try making it explicit:
from . import aux
I have set my path environment variable to include the python interpreter as well as my "python_scripts" folder. I can call the python interpreter or any .py files located in my "python_scripts" folder individually from anywhere on my machine as follows:
C:\> python.exe
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
or
C:\> test_code.py
and they both work fine. However, when I call the python interpreter followed by the script name:
C:\> python.exe test_code.py
it returns with the following error unless I'm located in my "python_scripts" folder:
python: can't open file 'test_code.py': [Errno 2] No such file or directory
Why would this be happening?
If you use C:\> python.exe test_code.py it will look in your current directory for test_code.py. If you pass an argument to python.exe it needs to be a valid absolute or relative path to a file that exists.
That means that the file is on your Windows %PATH% variable. When you do python.exe, it looks in all sorts of places. For example, if your PATH looked like: C:\;"C:\Program Files\Python 3\";C:\Users\user\python_scripts, it would try C:\python.exe, then try C:\Program Files\Python 3\python.exe and find Python.
When you do test_code.py, it finds it as C:\Users\user\python_scripts\test_code.py (For example).
When you call python.exe, it just reads the filename and does not try to resolve the path.
Python uses a different path for importing, which can be seen as sys.path.
You can extend this using a .pth file in python\Lib\site-packages.
For example, if you add a user_pth.pth file with this content:
C:\Users\user\python_scripts\
Then you can do import test_code from any file.
So, you can invoke pythons importer by running your code as a module:
python.exe -m test_code
you should give python.exe the full path to your script python.exe.
c:\test_code.py
I copied my environment variable as
C:\Python\Python38;C:\Users\ronegi\Desktop\PERSONAL\EDUREKA PYTHON
Now when I open Python.exe & after prompt I type as shown below
Hello_world.py
It throws error below:
I tried giving all tries.
I keep getting this (well known) error in iPython. Yet, the same import works fine in plain Python. (Python 3.3.5, see details below)
iPython:
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 2.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import test1
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-7-ddb30f03c287> in <module>()
----> 1 import test1
ImportError: DLL load failed: The specified module could not be found.
Python (not only it loads fine, it also works):
$ python
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import test1
>>>
Now, Dependency Walker on test1.pyd shows this
[ ? ] LIBGCC_S_DW2-1.DLL Error opening file. The system cannot find the file specified (2).
[ ? ] LIBSTDC++-6.DLL Error opening file. The system cannot find the file specified (2).
[ ? ] PYTHON33.DLL Error opening file. The system cannot find the file spec
I even overwrote sys.path in iPython with the one from plain Python. The file test1.pyd is in C:\Test.
['c:\\Test',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\python33.zip',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\DLLs',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\FontTools',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\win32',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\win32\\lib',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\Pythonwin']
Why would the import work in plain Python but not in iPython?
I have encountered the same problem. After hours looking and thinking I found out the cause. The difference is environment variables between interpreters (plain python and ipython or pycharm etc.). I think your can use %env in ipython to check the environment variables. In plain python, use (works in python 3.7):
import os
os.environ
Then if there are differences, maybe you should set the right one before your run.
Actually there are multiple ways to set envs. For example
os.environ['key']='value' #Both key and value are strings
or
os.putenv('key', 'value')
Here key is the name of the environment variable, and value is the value you want to set it to.
Hope this helps you.~~~///(^v^)\\~~~
I tried to install cx_Oracle from pypi source since there is no available port for it in cygwin. I did make some changes as suggested in http://permalink.gmane.org/gmane.comp.python.db.cx-oracle/2492 and modified my setup.py. However, I still get the following error :-
$ python
Python 2.7.3 (default, Dec 18 2012, 13:50:09)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
/usr/lib/python2.7/site-packages/cx_Oracle-5.1.3-py2.7-cygwin-1.7.24-i686.egg/cx_Oracle.py:3: UserWa
rning: Module cx_Oracle was already imported from /usr/lib/python2.7/site-packages/cx_Oracle-5.1.3-p
y2.7-cygwin-1.7.24-i686.egg/cx_Oracle.pyc, but /home/zerog/cx_Oracle-5.1.3 is being added to sys.pat
h
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.cygwin-1.7.24-i686/egg/cx_Oracle.py", line 7, in <module>
File "build/bdist.cygwin-1.7.24-i686/egg/cx_Oracle.py", line 6, in __bootstrap__
ImportError: Exec format error
>>>
If someone can please help me fix this ?
TIA.
Fixed this by specifying the path to instantclient as below :
$ export PATH=$PATH:/cygdrive/d/Tools/instantclient_11_2
(Other, possibly important stuff) :
$ echo $LD_LIBRARY_PATH
/cygdrive/d/Tools/instantclient_11_2
$ echo $ORACLE_HOME
/cygdrive/d/Tools/instantclient_11_2
Now, I get :-
$ python
Python 2.7.3 (default, Dec 18 2012, 13:50:09)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
It's hard to pin down from the error message alone, but I am guessing that you have two different copies of cx_Oracle in your sys.path. The error message is complaining that a different version of the same module had already been import-ed.
Presumably the pristine upstream version is installed system-wide in /usr/lib/python2.7/site-packages/cx_Oracle-5.1.3-py2.7-cygwin-1.7.24-i686.egg, and your modified version in /home/zerog/cx_Oracle-5.1.3.
Does it work if you pare down sys.path so that only the original, or only your modified version, is included?
(You might want to use virtualenv if you need to switch back and forth between two versions frequently.)
I ran into this error "Exec format error."
For me, this was likely caused by a mismatch between cygwin being installed as 64 bit, but the instant client being installed as 32 bit. Double check that everything (oracle, cygwin) is either 32 bit or 64 bit.
What fixed my issue:
Since my cygwin is 64 bit (see uname -a, and look for x86_64), I downloaded the 64 bit instant client from oracle's website, and unzipped
I set the env vars in .profile, to point where it was unzipped:
export ORACLE_HOME=/cygdrive/c/oracle/instantclient_x64_11_2
export LD_LIBRARY_PATH=$ORACLE_HOME
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export TNS_ADMIN='//optional/path/to/your/oracle/tns/files/'
source ~/.profile
To test, you should now be able to run this python command with no error:
import cx_Oracle
To verify the path is correct, if you run ls, you should see something like
ls $ORACLE_HOME
adrci.exe genezi.exe oci.sym ociw32.dll ojdbc6.jar
oraocci11.dll oraociei11.sym uidrvci.exe vc9
adrci.sym genezi.sym ocijdbc11.dll ociw32.sym orannzsbb11.dll
oraocci11.sym orasql11.dll uidrvci.sym xstreams.jar
BASIC_README oci.dll ocijdbc11.sym ojdbc5.jar orannzsbb11.sym
oraociei11.dll orasql11.sym vc8
Here's my setup: a Mac, running OS X Tiger. Windows XP running in a virtual machine (Parallels). Windows XP has my Mac home directory mapped as a network drive.
I have two files in a directory of my Mac home directory:
foo.py
pass
test.py
import foo
If I run test.py from within my virtual machine by typing 'python test.py', I get this:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import foo
ImportError: No module named foo
If I try to import foo from the console (running python under Windows from the same directory), all is well:
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
>>>
If I run test.py with Mac python, all is well.
If I copy test.py and foo.py to a different directory, I can run test.py under Windows without problems.
There is an init.py in the original directory, but it is empty. Furthermore, copying it with the other files doesn't break anything in the previous paragraph.
There are no python-related environment variables set.
Any ideas?
Add import sys; print sys.path to the start of test.py. See what it prints out in the failing case. If "." isn't on the list, that may be your problem.
As a random guess: are the permissions on foo.py accessable from the windows client? (eg try opening with notepad from the virtual machine).
If that's OK, try running:
python -v -v test.py
and looking at the output (alternatively, set PYTHONVERBOSE=2). This should list all the places it tries to import foo from. Comparing it with a similar trace on the working machine may give some further clues.