0

There's some code I'd like to use from GitHub on my Windows 10 machine. The error I keep getting is:

AttributeError: module 'os' has no attribute 'fork'

I looked this up and found that Windows doesn't support the os.fork() call, so I tried looking into ways to get bash on a Windows machine (I might need bash for other scripts in the future). I discovered the Windows Linux Subsystem, and it sounded exactly right for this. I installed WLS1 and got Ubuntu for it from the Microsoft store. Bash seems to work as expected, but this script still throws the same error in bash.

I've found a similar question from a few years ago (how to run python script with os.fork on windows?). The accepted answer recommended running a Linux application on a virtual machine, which I think is exactly what I'm doing (right?). Does anyone know how I can get this script to work? (Btw I know it works bc I've run it before on my Linux machine at work.)


Edit: This might be a symptom of another problem.

I think this means Ubuntu WSL1 is using the Linux path to python:

In Powershell: Get-Command python gives me C:\Users\myname\AppData\Local\Microsoft...

In Ubuntu: which python gives me /usr/bin/python3

..

In Powershell, if I try to read the .toml file using wellmap .\wellmap-text.toml, I get the "not in Path" error (which I've been seeing for a lot of things, as I try to set up my machine, and I'm unfamiliar with the right way of adding things to my Path or if doing so could break something)

wellmap : The term 'wellmap' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ wellmap .\wellmap-text.toml
+ ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (wellmap:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

On the Ubuntu WSL, running wellmap ./wellmap-text.toml gives me a command not found error.

..

Because I know the Path to the wellmap.exe, I tried to give Powershell and Ubuntu that Path directly and be super explicit. This is where I get the module 'os' has no attribute 'fork' error.

In Powershell:

& “C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Scripts\wellmap.exe” \Users\myname\Desktop\wellmap-text.toml

And this error:

Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 87, in run_code exec(code, run_globals) File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Scripts\wellmap.exe_main.py", line 7, in File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\wellmap\plot.py", line 101, in main if os.fork() != 0: AttributeError: module 'os' has no attribute 'fork'

In Ubuntu:

/mnt/c/Users/myname/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/Scripts/wellmap.exe /mnt/c/Users/myname/Desktop/wellmap-test.toml

And the same error too:

Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\lib\runpy.py", lin e 197, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\lib\runpy.py", lin e 87, in run_code exec(code, run_globals) File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-package s\Python39\Scripts\wellmap.exe_main.py", line 7, in File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-package s\Python39\site-packages\wellmap\plot.py", line 101, in main if os.fork() != 0: AttributeError: module 'os' has no attribute 'fork'

rcpi
  • 41
  • 7

3 Answers3

0

You may want to specify the script, the Linux distribution on the WSL (and version). Just tried it on Ubuntu 20.04 on WSL2 with Python 3.8.5. The following lines

import os
os.fork()

work fine.

whytong
  • 11
  • 2
  • the script is called [wellmap](https://wellmap.readthedocs.io/en/latest/file_format.html) the Ubuntu I downloaded is Version 2004.2020.812.0 I just downloaded WSL1, but I don't know how to get the version of it. I'm using Python 3.8.2. – rcpi Jan 24 '21 at 01:37
  • I tried it on Ubuntu 20.04 on WSL2 with X-server. Works fine. – whytong Jan 24 '21 at 01:56
0

The information you've added to the updated question highlights a likely problem. You say that in Ubuntu, you are running:

/mnt/c/Users/myname/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/Scripts/wellmap.exe /mnt/c/Users/myname/Desktop/wellmap-test.toml

If that's, the case, then you are still running the Windows version of wellmap.exe under Ubuntu. Because it's running the exe, WSL passes control back to Windows (it's a bit more complicated than this). Regardless, if you are running the .exe, then it's running in the Windows Python environment, not under Linux Python.

Let's try actually installing and running the Linux Python wellmap. We can do this in a virtual environment, to prevent changing the default Python environment. In Ubuntu:

cd ~ # Or whever you want to set up this virtual environment
python -m venv wellmap-venv # since we've already confirmed that this points to the Linux version
. wellmap-venv/bin/activate
pip install wellmap

And then try wellmap (without an extension) with your .toml file. That .toml file can still be in a Windows path without impact.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
  • Ah I see what you're saying! Okay, I like the idea of making an environment and keeping any changes contained to that. I tried `python -m venv wellmap-venv` in Ubuntu, but it didn't understand what "python" meant. I changed that to "python3" and got an error: `The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command. apt-get install python3-venv` Am I doing this in the wrong place? I'm scared of breaking things. (Also thanks so much for helping me out!) – rcpi Jan 25 '21 at 04:01
  • Right - Apologies, I should have remembered that venv isn't there by default. Yes, do a `sudo apt-get install python3-venv` and then try the commands in my answer. – NotTheDr01ds Jan 25 '21 at 17:27
  • Also, read up on venv a bit. It's fairly straightforward, you just need to know to "activate" the virtual environment before you use it (this will set your path to the right Python, pip, etc. version in that subshell), and then "deactivate" when done. – NotTheDr01ds Jan 25 '21 at 17:29
-1

Since you mentioned that you tried running this on Python on Windows first, it's likely that your Windows Python version is still taking priority over your Linux Python.

Check the Python location with which python3. You'll probably find it pointing to the Windows version.

Check your path in Ubuntu. You'll probably find that the Windows Python version is there, and it has precedence over the Linux version. This is because WSL "helpfully" prepends the Windows path to the Linux path to allow you to access your Windows apps and utilities under Linux. This is useful in most cases, but can cause problems when both the Windows and Linux version of a tool are installed.

Assuming that is the case, there are quite a few workarounds that I know of. Three of them, you can read about in my answer here regarding a similar problem with npm on WSL. They are all applicable to Python just as much as nodejs/npm.

Of course, you can also just specify the full path to the Linux Python, /usr/bin/python3, when launching your app.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
  • I think Ubuntu is using the right python, because when I do "which python" it tells me `/usr/bin/python3`, which it ought to be. I added more detail to the actual commands I'm trying to run and the errors I'm seeing above in my question. – rcpi Jan 25 '21 at 00:45