Python Forum
switch from version 3.5.3 to 3.8.6 get import error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
switch from version 3.5.3 to 3.8.6 get import error
#1
I am relatively new to Python. I have two small py programs. It runs on 3.5.3. When I switch to a newer version of Python (version 3.8.6). I got the following error:

File ".\resources\scripts\batchEditProtocolXML.py", line 1, in <module>
import replaceLineInFiles
ModuleNotFoundError: No module named 'replaceLineInFiles'


after some research, I changed the import statement. Then I got the following error:

File ".\resources\scripts\batchEditProtocolXML.py", line 1, in <module>
from .replaceLineInFiles import replace
ImportError: attempted relative import with no known parent package


I know there are some big differences between Python 2 and 3. But I am surprised that the differences between 3.5 and 3.8 are not small. Does this mean I have to create a package/module on each file/unit/groupOfFunctions? Anywhere (books, online sites) I can find the information about the differences between major release versions?
Reply
#2
What's your file structure? Where is replaceLineInFiles.py compared to the file you're running? You're correct, I wouldn't expect any major differences with how the imports are run.

How are you invoking the different versions of python? Is there any chance you have a wrapper for one of them that sets PYTHONPATH with the location of your module?

In both versions of python run:
import sys
print(sys.path)
Are they the same? Is the location of the module in the 3.5 path but not the 3.8 path?
Reply
#3
(Oct-30-2020, 05:47 PM)bowlofred Wrote: What's your file structure?

How are you invoking the different versions of python? Is there any chance you have a wrapper for one of them that sets PYTHONPATH with the location of your module?

In both versions of python run:
import sys
print(sys.path)
Are they the same? Is the location of the module in the 3.5 path but not the 3.8 path?

I am using Win7. All my py files are under the same directory at the same level.

I didn't install the Python. I unzipped the download Python release and put them under a "win" folder. I have a batch file to launch the terminal and set PYTHON_HOME and PYTHONPATH for that terminal automatically. Then I run python on that terminal.

When I switch python version. I just make sure the "win" folder is the correct version by rename corresponding folders.

For 3.5 version, I got the following print out about the sys.path:

['', 'D:\\MyFolder\\3rdparty\\python\\win\\python35.zip',
'D:\\MyFolder\\3rdparty\\python\\win\\DLLs',
'D:\\MyFolder\\3rdparty\\python\\win\\lib',
'D:\\MyFolder\\3rdparty\\python\\win']


For 3.8 version, the result is this:

['', 'D:\\MyFolder\\3rdparty\\python\\win\\python38.zip',
'D:\\MyFolder\\3rdparty\\python\\win']


I don't think there are any differences because there are no "DLLs" and "lib" folders for version 3.5.
Reply
#4
(Oct-30-2020, 08:12 PM)python001 Wrote: All my py files are under the same directory at the same level.

Is that directory D:\\MyFolder\\3rdparty\\python\\win? If it's there, it should be picked up. If it's not, then it's because it's not in your PYTHONPATH.
Reply
#5
(Oct-30-2020, 08:58 PM)bowlofred Wrote:
(Oct-30-2020, 08:12 PM)python001 Wrote: All my py files are under the same directory at the same level.

Is that directory D:\\MyFolder\\3rdparty\\python\\win? If it's there, it should be picked up. If it's not, then it's because it's not in your PYTHONPATH.

You are right. My py files are not in the PYTHONPATH based on the printout. They are under another directory physically. I start the Windows terminal with a few SET commands. So in the Windows terminal, PYTHON_HOME and PYTHONPATH are set corrected (checked with echo). The Python path and my py folder path are both in the PYTHONPATH:

D:\MyFolder>echo %pythonpath%
;.;D:\MyFolder\3rdparty\python\win;D:\MyFolder\resources\scripts


Then I run my py with "python .\resources\scripts\batchEditProtocolXML.py" from that terminal. It seems like the temporary environment for that Windows terminal is not passed onto the Python. There must be something wrong here. How can I pass the environment settings to the Python?
Reply
#6
That does seem a bit odd. I confirmed on my windows10 it seems to be working as expected.

Do you see changes when you run the command this way?

C:\> set PYTHONPATH=C:\TEMP
C:\> python -c "import sys; import os; print(os.environ['PYTHONPATH']); print(sys.path)"
Reply
#7
(Oct-31-2020, 12:49 AM)bowlofred Wrote: That does seem a bit odd. I confirmed on my windows10 it seems to be working as expected.

Do you see changes when you run the command this way?

C:\> set PYTHONPATH=C:\TEMP
C:\> python -c "import sys; import os; print(os.environ['PYTHONPATH']); print(sys.path)"

The output is this:

C:\TEMP
['D:\\MyFolder\\3rdparty\\python\\win\\python38.zip', 'D:\\MyFolder\\3rdparty\\python\\win']



Repeat previous test, looks like os.environ has my custom settings:
D:\MyFolder>python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
>>> import os
>>> import sys
>>> print(os.environ['PYTHONPATH']); print(sys.path)
;.;D:\MyFolder\3rdparty\python\win;D:\MyFolder\resources\scripts
['D:\\MyFolder\\3rdparty\\python\\win\\python38.zip', 'D:\\MyFolder\\3rdparty\\python\\win']
>>>
Reply
#8
version 3.8 is also not easy to terminate Python process:


D:\MyFolder>python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
>>> os.exit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> sys.exit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> exit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'exit' is not defined
>>> exit
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'exit' is not defined
>>> quit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'quit' is not defined
>>> import sys
>>> sys.exit()

D:\MyFolder>
Reply
#9
Can you see if you get any difference when run with -S? python -S -c "import sys ; print(sys.path)". If you do get something different, might be good to see python -S -m site output.

In a standard windows command shell, I would normally exit with a control-Z.
Reply
#10
(Oct-31-2020, 03:10 AM)bowlofred Wrote: Can you see if you get any difference when run with -S? python -S -c "import sys ; print(sys.path)".

The output is the same as the others.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  import module with syntax error Skaperen 7 616 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  Import Error Christian 3 892 Apr-17-2021, 11:27 PM
Last Post: bowlofred
  Error on Python Version? ErnestTBass 6 1,035 Dec-09-2020, 04:02 PM
Last Post: ErnestTBass
  Folium import error kashif_flavio 2 855 Dec-07-2020, 08:58 AM
Last Post: bowlofred
  Import error Beingcode 0 913 Nov-20-2020, 02:57 AM
Last Post: Beingcode
Smile Import error with local file colt 1 624 Nov-08-2020, 07:56 AM
Last Post: Gribouillis
  import yaml error for python3 maiya 4 5,529 Jul-15-2020, 06:07 PM
Last Post: Gribouillis
  Which app do I import to fix this error? ErnestTBass 6 1,794 Apr-26-2020, 08:43 PM
Last Post: snippsat
  Error while humansize module import. johnkgson 3 1,497 Apr-26-2020, 08:37 AM
Last Post: johnkgson
  Importing module from a package results in import error goghvv 2 875 Mar-27-2020, 07:13 PM
Last Post: goghvv

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020