Python 3.x 如何设置transcrypt

Python 3.x 如何设置transcrypt,python-3.x,transcrypt,Python 3.x,Transcrypt,我遵循由提供的入门部分,创建了一个名为hello的文件夹,然后创建了两个文件hello.py,hello.html <script type="module">import * as hello from './__target__/hello.js'; window.hello = hello;</script> <p> <div id = "FUN">...</div>

我遵循由提供的入门部分,创建了一个名为hello的文件夹,然后创建了两个文件
hello.py
hello.html

<script type="module">import * as hello from './__target__/hello.js'; window.hello = hello;</script>
    <p>
    <div id = "FUN">...</div>
    <button onclick="hello.FUN()">Do code</button>
我的目标是在客户端运行一个函数,并将结果输出到网页

hello.py

from datetime import datetime
import time
import random

def FUN():
    for i in range(random.randrange(1, 9)):
        time.sleep(random.randrange(10 , 30 ))
        print (f"Current time {datetime.now()} , Number {i}")


hello.html

<script type="module">import * as hello from './__target__/hello.js'; window.hello = hello;</script>
    <p>
    <div id = "FUN">...</div>
    <button onclick="hello.FUN()">Do code</button>
我使用命令
pip3 Install transcrypt安装transcrypt

但是当我运行这个命令时,
python-mtrancrypt-b-m-nhello.py

from datetime import datetime
import time
import random

def FUN():
    for i in range(random.randrange(1, 9)):
        time.sleep(random.randrange(10 , 30 ))
        print (f"Current time {datetime.now()} , Number {i}")


它使用此输出中止

Saving target code in: /home/kali/n3w/__target__/org.transcrypt.__runtime__.js
Saving target code in: /home/kali/n3w/__target__/re.translate.js
Saving target code in: /home/kali/n3w/__target__/re.js
Saving target code in: /home/kali/n3w/__target__/warnings.js

Error while compiling (offending file last):
        File '/home/kali/n3w/hello.py', line 1, at import of:
        File '/home/kali/.local/lib/python3.9/site-packages/wget.py', line 562, at import of:
        File '/usr/lib/python3.9/optparse.py', line 90, at import of:
        File '/usr/lib/python3.9/gettext.py', line 718, at import of:
        File '/usr/lib/python3.9/locale.py', line 658, at import of:
        File '_bootlocale', line 8, namely:

        Import error, can't find any of:
                /home/kali/n3w/locale.py
                /home/kali/n3w/locale.js
                /home/kali/.local/lib/python3.9/site-packages/transcrypt/modules/locale.py
                /home/kali/.local/lib/python3.9/site-packages/transcrypt/modules/locale.js
                /home/kali/n3w/locale.py
                /home/kali/n3w/locale.js
                /home/kali/n3w/_bootlocale.py
                /home/kali/n3w/_bootlocale.js
                /home/kali/.local/lib/python3.9/site-packages/transcrypt/modules/_bootlocale.py
                /home/kali/.local/lib/python3.9/site-packages/transcrypt/modules/_bootlocale.js
                /home/kali/n3w/_bootlocale.py
                /home/kali/n3w/_bootlocale.js
                /home/kali/n3w/_locale.py
                /home/kali/n3w/_locale.js
                /home/kali/.local/lib/python3.9/site-packages/transcrypt/modules/_locale.py
                /home/kali/.local/lib/python3.9/site-packages/transcrypt/modules/_locale.js
                /home/kali/n3w/_locale.py
                /home/kali/n3w/_locale.js
                /usr/lib/python3.9/_locale.py
                /usr/lib/python3.9/_locale.js
                /usr/lib/python3.9/lib-dynload/_locale.py
                /usr/lib/python3.9/lib-dynload/_locale.js
                /home/kali/.local/lib/python3.9/site-packages/_locale.py
                /home/kali/.local/lib/python3.9/site-packages/_locale.js
                /home/kali/testing_0505/npm_socket/batavia/_locale.py
                /home/kali/testing_0505/npm_socket/batavia/_locale.js
                /usr/local/lib/python3.9/dist-packages/_locale.py
                /usr/local/lib/python3.9/dist-packages/_locale.js
                /usr/lib/python3/dist-packages/_locale.py
                /usr/lib/python3/dist-packages/_locale.js
                /usr/lib/python3.9/dist-packages/_locale.py
                /usr/lib/python3.9/dist-packages/_locale.js



Transcrypt站点提供了如何设置这个小演示的步骤,但没有详细介绍每个命令的作用。新版本正在制作中,但可能需要一段时间。向你问好,雅克·德·霍奇这里有一些信息,也许会有帮助

  • 您的系统还安装了Python3.9,当您键入“Python”时,它会优先使用

    要使用版本3.7,您可能必须键入“python3.7”或类似的命令,具体取决于您使用的操作系统。要查看键入“python”时使用的版本,可以使用:

      python --version
    
    要查看python的位置(您可能也可以在这里找到3.7版本),请使用以下命令:

      whereis python  # depends on your OS
    
  • 用一个简单的方法避免不必要的问题

    e、 g:使用内置的“venv”模块:

      # within your project folder:
      python3.7 -m venv package_dir
    
      # The above creates a directory called package_dir,
      # where local project python packages can go.
      # Then 'activate', after which you can use 'python'
      # to refer to the version of python in the package_dir,
      # and 'pip' to refer to pip3 etc.:
      source package_dir/bin/activate
    
      # Will now install transcrypt in package_dir, not globally.
      pip install transcrypt
    
  • Transcrypt不包含很多标准库,在这些情况下可以使用js。在浏览器选项中权衡各种python的优缺点

    例如,我注意到了,但randint是。有关兴趣,请参阅中的randrange,这暗示了为什么它不在Transcrypt中。randint(a,b)与randrange(a,b+1)相同

    从Transcrypt的时间实现来看,睡眠被排除在外,因为在当时,在浏览器中处理它需要一个循环(阻塞)

因此,假设您想使用Transcrypt,因为它的性能和占地面积小,并且决定randint适合您的需要,并且您愿意使用,下面是它的外观:

from datetime import datetime
import random

# Add a js function that can be called by your python code.
__pragma__ ('js', '{}', '''
// Will not block main thread.
function async_sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms * 1000));
}
''')

# We're using async / await to avoid blocking the main thread.
async def FUN():
    for i in range(random.randint(1, 9 - 1)):
        await async_sleep(random.randint(10 , 30 - 1))
        current_time = f"Current time {datetime.now()} , Number {i}"
        # To output to the DOM, use js DOM functions.
        document.getElementById('FUN').innerHTML += current_time + '<br>'
从日期时间导入日期时间
随机输入
#添加一个可由python代码调用的js函数。
__布拉格语('js','{}','
//不会阻塞主线程。
函数异步睡眠(ms){
返回新承诺(resolve=>setTimeout(resolve,ms*1000));
}
''')
#我们使用async/await来避免阻塞主线程。
async def FUN():
对于范围内的i(random.randint(1,9-1)):
等待异步睡眠(random.randint(10,30-1))
current_time=f“当前时间{datetime.now()},数字{i}”
#要输出到DOM,请使用js DOM函数。
document.getElementById('FUN')。innerHTML+=当前时间+'
'
作为比较,线程阻塞版本的sleep可能是这样的

from datetime import datetime
import random

__pragma__ ('js', '{}', '''
// Bad, will block main thread.
function blocking_sleep(sec) {
    var unixtime_ms = new Date().getTime();
    while(new Date().getTime() < unixtime_ms + sec * 1000) {}
}
''')

def FUN():
    for i in range(random.randint(1, 9 - 1)):
        blocking_sleep(random.randint(10 , 30 - 1))
        current_time = f"Current time {datetime.now()} , Number {i}"
        # print() outputs to the dev console.
        print (current_time)
        document.getElementById('FUN').innerHTML += current_time + '<br>'
从日期时间导入日期时间
随机输入
__布拉格语('js','{}','
//坏,将阻塞主线程。
功能阻塞睡眠(秒){
var unixtime_ms=new Date().getTime();
while(new Date().getTime()'

您提到Transcrypt目前在Python3.7中工作。我认为问题在于如何设置它。如果你能回答这个问题,那会很有帮助的!安装还可以,只是解释器版本我在使用python3.7.6