Why does function with 2 parameters throw "myfunction() takes 1 positional argument but 2 were given"

5971
5
Jump to solution
07-12-2019 04:41 AM
RichardReinicke
Occasional Contributor II

Hello,

at the moment I'm testing python scripts in ArcGIS Pro, a very basic technique. But because ArcGIS Pro 2.3 works with python 3.6 in my case, I wanted to structure my code a bit more leveraging classes, modules, packages.

So my project structure is as follows:

project_folder\
    mypackage\

        __init__.py

        process.py
    main.py

my __init__.py

import mypackage.process

process.py looks as follows, note without classes !!!

import arcpy


def run_process(param1, param2):
    try:
        arcpy.AddMessage(param1 + " " +param2)
    except:
        arcpy.AddError(arcpy.GetMessages(2))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

in my main.py I do

import mypackage.process as process‍

and then in my code I call run_process() method:

process.run_process('test', 'another test')

Now this runs perfectly if I start the script from my IDE. I pass two arguments to a function with two parameters BUT

once I call the script from within ArcGIS Pro -> Toolbox -> Script, I get the following error message:

TypeError: run_process() takes 1 positional argument but 2 were given

I can't get behind this error message and was researching on this problem a lot. Most occurences of such errors are related to method inside instances passing themself as additional first argument automatically but this shouln't be the case here. 

Moreover it tells me that my run_process() function would have only 1 parameter what is definetly not the case. The function takes 2 parameters!

Can someone please help me to reproduce this?

0 Kudos
1 Solution

Accepted Solutions
PeteCrosier
Occasional Contributor III

Have you tried closing and re-opening ArcGIS Pro? I've just seen it ignore changes I've been making to my scripts when using this kind of 'import' setup - specifically ignoring changes to the code I'm importing but seeing changes to the code doing the import. I closed and re-opened Pro and it started working as expected, until I made more changes. Seems like it's caching imported code somehow?

View solution in original post

5 Replies
PeteCrosier
Occasional Contributor III

Have you tried closing and re-opening ArcGIS Pro? I've just seen it ignore changes I've been making to my scripts when using this kind of 'import' setup - specifically ignoring changes to the code I'm importing but seeing changes to the code doing the import. I closed and re-opened Pro and it started working as expected, until I made more changes. Seems like it's caching imported code somehow?

LanceCole
MVP Regular Contributor

Richard, 

Take a look at your project structure folder using a file explorer and see if there is a process.pyc file.  Delete this file and try to run the script again.  Also, there is a python method called Process with a capital “P”, be careful using similar naming in your code.  

RichardReinicke
Occasional Contributor II

Hello Peter, Hello Lance,

thank you both for your helpful comments. It turned out that reopening ArcGIS Pro indeed solved my problem and it may somehow also relate to the __pycache__ subfolders with *.pyc files I have. But because I have the __pycache__ folders in my projects which I would have to delete over and over, I decided that reopening ArcGIS Pro is the more "comfortable" option.

Of course it`s still annoying and it would be interresting if there's a way to circumvent this problem and to make ArcGIS Pro always run the latest local version.

0 Kudos
LanceCole
MVP Regular Contributor

Richard, 

Try adding the following code as the first two lines in your scripts even before your other import statements.  This will prevent the pyc files from being generated.

import sys
sys.dont_write_bytecode = True‍‍

You will need to delete the pyc files if they have already been created but the script will no longer generate new pyc files.  There may still be issues with the modules you are importing, try adding these two lines to these as well.

0 Kudos
LukeWebb
Occasional Contributor III

I think ArcGIS itself just caches the entire script, .py or .pyc, the first time it is used in a session.

Its really annoying for me as a developer, but there are work arounds when developing, such as rename your .tbx or .py file, so arcgis makes a new cache for this entirely new file.

Apart from during development, it really shouldnt be an issue, as if you are making regular changes to part of your code, then that part should probably be a tool parameter!

0 Kudos