Unresolved import 'anvil.server'

Hello, i am trying to configure an uplink server and i have this code on visual studio:

import anvil.server
import csv

@anvil.server.callable
def load_csv_data():
    some_data = []
   
    with open(r"C:\Users\Tiago\Desktop\valoresm2.csv", newline='') as csvfile:
        csvreader = csv.reader(csvfile)
        for row in csvreader:
            some_data.append(row)
    return some_data


anvil.server.connect("----------------mykey--------------------")

try:
 
    anvil.server.wait_forever()

except KeyboardInterrupt:

    print("User called to quit.")

    input("<< PRESS RETURN >>")

    exit()

I have the correct key on the mykey place.
Im having an unresolved import error on the first line, i already pip installed anvil and pip installed anvil-uplink, i tried this code on python 3.9 and 3.7…
Anyone can help me?

Did you
python -m pip install anvil-uplink
after you had already started visual studio? If so, you might want to restart to make the linter recognize the package.

Also possible that your visual studio is not pointing to the same version of python as you have installed anvil-server on?

These are both shots in the dark, but I have done this on a few machines and had the same first time use errors as you described.

i did that now and restarted and i have the same error.

Ok lets troubleshoot:

if you have python on windows you probably have IDLE, if this works, anvil is installed correctly:
with:
image

idle_uplink

So if this works, it is (more likely) visual studio not configured correctly, if it does not work then it is anvil installed incorrectly somehow.

i did that and i get an error on anvil.server import:

import anvil.server
Traceback (most recent call last):
File “<pyshell#0>”, line 1, in
import anvil.server
ModuleNotFoundError: No module named ‘anvil’

Ok try:

Elevated command prompt (search for ‘cmd’ right-click- ‘run as administrator’)
image

python -m pip install anvil-uplink

you might have multiple versions of python installed, and pip may be installing it under the incorrect PATH
python -m usually fixes this, but not always

mine is installed already obviously so it just tells me it is already installed.

yeah i got the same result after running that command

ok so if you just run whatever python is in your path as ‘python’:

image

does the version match the one you used in IDLE?

python --version will give you the same information also.

You could try checking what your PATH is by using the instructions here:

you could also look up where your visual studio is pointing to python? Is it just VS, or VS Code?

my python version is 3.9.1 and i am using microsoft visual studio yes, i have both 3.9 and 3.7 IDLE and anvil.server import has errors on both

a problem could be that you seem to have pip installed both anvil and anvil-uplink

these may have conflicts since the pypi package named anvil is not associated with anvil.works.

Things to try:

pip uninstall anvil
pip3 install anvil-uplink 
# sometimes pip3 may be required depending on how python is running locally

also check out this thread

in your script

import anvil
print(anvil.__file__) 
# you may not be importing the anvil-uplink pypi package but something else
# go to this file and see what it is - change the name of the file, rinse and repeat
print(dir(anvil))
# BlobMedia, Button, ...
# if you don't get the obvious anvil dir then you are importing the wrong anvil

You can also try running the uplink script in a virtual environment.
The advantage here is that you install packages within the virtual environment rather than globally.